tpm2_tss_constants.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright 2016 The Chromium OS Authors. All rights reserved.
  3. * Use of this source code is governed by a BSD-style license that can be
  4. * found in the LICENSE file.
  5. *
  6. * Some TPM constants and type definitions for standalone compilation for use
  7. * in the firmware
  8. */
  9. #ifndef __VBOOT_REFERENCE_FIRMWARE_INCLUDE_TPM2_TSS_CONSTANTS_H
  10. #define __VBOOT_REFERENCE_FIRMWARE_INCLUDE_TPM2_TSS_CONSTANTS_H
  11. #define TPM_BUFFER_SIZE 256
  12. /* Tpm2 command tags. */
  13. #define TPM_ST_NO_SESSIONS 0x8001
  14. #define TPM_ST_SESSIONS 0x8002
  15. /* TPM2 command codes. */
  16. #define TPM2_Hierarchy_Control ((TPM_CC)0x00000121)
  17. #define TPM2_Clear ((TPM_CC)0x00000126)
  18. #define TPM2_NV_DefineSpace ((TPM_CC)0x0000012A)
  19. #define TPM2_NV_Write ((TPM_CC)0x00000137)
  20. #define TPM2_NV_WriteLock ((TPM_CC)0x00000138)
  21. #define TPM2_SelfTest ((TPM_CC)0x00000143)
  22. #define TPM2_Startup ((TPM_CC)0x00000144)
  23. #define TPM2_Shutdown ((TPM_CC)0x00000145)
  24. #define TPM2_NV_Read ((TPM_CC)0x0000014E)
  25. #define TPM2_NV_ReadLock ((TPM_CC)0x0000014F)
  26. #define TPM2_NV_ReadPublic ((TPM_CC)0x00000169)
  27. #define TPM2_GetCapability ((TPM_CC)0x0000017A)
  28. /* TCG Spec defined, verify for TPM2.
  29. * TODO(apronin): find TPM2 RC substitutes for TPM1.2 error codes.
  30. */
  31. #define TPM_E_BADINDEX ((uint32_t) 0x00000002)
  32. #define TPM_E_INVALID_POSTINIT ((uint32_t) 0x00000026)
  33. #define TPM_E_BADTAG ((uint32_t) 0x0000001E)
  34. #define TPM_E_IOERROR ((uint32_t) 0x0000001F)
  35. #define TPM_E_MAXNVWRITES ((uint32_t) 0x00000048)
  36. #define HR_SHIFT 24
  37. #define TPM_HT_NV_INDEX 0x01
  38. #define HR_NV_INDEX (TPM_HT_NV_INDEX << HR_SHIFT)
  39. #define TPM_RH_OWNER 0x40000001
  40. #define TPM_RH_PLATFORM 0x4000000C
  41. #define TPM_RS_PW 0x40000009
  42. /* TPM2 capabilities. */
  43. #define TPM_CAP_FIRST ((TPM_CAP)0x00000000)
  44. #define TPM_CAP_TPM_PROPERTIES ((TPM_CAP)0x00000006)
  45. /* TPM properties */
  46. #define TPM_PT_NONE ((TPM_PT)0x00000000)
  47. #define PT_GROUP ((TPM_PT)0x00000100)
  48. #define PT_FIXED PT_GROUP
  49. #define PT_VAR (PT_GROUP * 2)
  50. #define TPM_PT_PERMANENT (PT_VAR + 0)
  51. #define TPM_PT_STARTUP_CLEAR (PT_VAR + 1)
  52. /* TPM startup types. */
  53. #define TPM_SU_CLEAR ((TPM_SU)0x0000)
  54. #define TPM_SU_STATE ((TPM_SU)0x0001)
  55. /* TPM algorithm IDs. */
  56. #define TPM_ALG_SHA1 ((TPM_ALG_ID)0x0004)
  57. #define TPM_ALG_SHA256 ((TPM_ALG_ID)0x000B)
  58. #define TPM_ALG_NULL ((TPM_ALG_ID)0x0010)
  59. /* NV index attributes. */
  60. #define TPMA_NV_PPWRITE ((TPMA_NV)(1UL << 0))
  61. #define TPMA_NV_OWNERWRITE ((TPMA_NV)(1UL << 1))
  62. #define TPMA_NV_AUTHWRITE ((TPMA_NV)(1UL << 2))
  63. #define TPMA_NV_POLICYWRITE ((TPMA_NV)(1UL << 3))
  64. #define TPMA_NV_MASK_WRITE (TPMA_NV_PPWRITE | TPMA_NV_OWNERWRITE |\
  65. TPMA_NV_AUTHWRITE | TPMA_NV_POLICYWRITE)
  66. #define TPMA_NV_PPREAD ((TPMA_NV)(1UL << 16))
  67. #define TPMA_NV_OWNERREAD ((TPMA_NV)(1UL << 17))
  68. #define TPMA_NV_AUTHREAD ((TPMA_NV)(1UL << 18))
  69. #define TPMA_NV_POLICYREAD ((TPMA_NV)(1UL << 19))
  70. #define TPMA_NV_MASK_READ (TPMA_NV_PPREAD | TPMA_NV_OWNERREAD |\
  71. TPMA_NV_AUTHREAD | TPMA_NV_POLICYREAD)
  72. #define TPMA_NV_PLATFORMCREATE ((TPMA_NV)(1UL << 30))
  73. /* Starting indexes of NV index ranges, as defined in "Registry of reserved
  74. * TPM 2.0 handles and localities".
  75. */
  76. #define TPMI_RH_NV_INDEX_TPM_START ((TPMI_RH_NV_INDEX)0x01000000)
  77. #define TPMI_RH_NV_INDEX_PLATFORM_START ((TPMI_RH_NV_INDEX)0x01400000)
  78. #define TPMI_RH_NV_INDEX_OWNER_START ((TPMI_RH_NV_INDEX)0x01800000)
  79. #define TPMI_RH_NV_INDEX_TCG_OEM_START ((TPMI_RH_NV_INDEX)0x01C00000)
  80. #define TPMI_RH_NV_INDEX_TCG_WG_START ((TPMI_RH_NV_INDEX)0x01C40000)
  81. #define TPMI_RH_NV_INDEX_RESERVED_START ((TPMI_RH_NV_INDEX)0x01C90000)
  82. typedef uint8_t TPMI_YES_NO;
  83. typedef uint32_t TPM_CC;
  84. typedef uint32_t TPM_HANDLE;
  85. typedef TPM_HANDLE TPMI_RH_NV_INDEX;
  86. typedef TPM_HANDLE TPMI_RH_ENABLES;
  87. typedef uint32_t TPM_CAP;
  88. typedef uint32_t TPM_PT;
  89. typedef uint16_t TPM_SU;
  90. typedef uint16_t TPM_ALG_ID;
  91. typedef TPM_ALG_ID TPMI_ALG_HASH;
  92. typedef uint32_t TPMA_NV;
  93. typedef struct {
  94. uint16_t size;
  95. uint8_t *buffer;
  96. } TPM2B, TPM2B_DIGEST, TPM2B_AUTH, TPM2B_NAME;
  97. typedef union {
  98. struct {
  99. uint16_t size;
  100. const uint8_t *buffer;
  101. } t;
  102. TPM2B b;
  103. } TPM2B_MAX_NV_BUFFER;
  104. typedef struct {
  105. TPM_PT property;
  106. uint32_t value;
  107. } TPMS_TAGGED_PROPERTY;
  108. typedef struct {
  109. uint32_t count;
  110. TPMS_TAGGED_PROPERTY tpm_property[1];
  111. } TPML_TAGGED_TPM_PROPERTY;
  112. typedef union {
  113. TPML_TAGGED_TPM_PROPERTY tpm_properties;
  114. } TPMU_CAPABILITIES;
  115. typedef struct {
  116. TPM_CAP capability;
  117. TPMU_CAPABILITIES data;
  118. } TPMS_CAPABILITY_DATA;
  119. typedef struct {
  120. TPMI_RH_NV_INDEX nvIndex;
  121. TPMI_ALG_HASH nameAlg;
  122. TPMA_NV attributes;
  123. TPM2B authPolicy;
  124. uint16_t dataSize;
  125. } TPMS_NV_PUBLIC;
  126. struct tpm2_nv_define_space_cmd {
  127. TPM2B auth;
  128. TPMS_NV_PUBLIC publicInfo;
  129. };
  130. struct tpm2_nv_read_cmd {
  131. TPMI_RH_NV_INDEX nvIndex;
  132. uint16_t size;
  133. uint16_t offset;
  134. };
  135. struct tpm2_nv_write_cmd {
  136. TPMI_RH_NV_INDEX nvIndex;
  137. TPM2B_MAX_NV_BUFFER data;
  138. uint16_t offset;
  139. };
  140. struct tpm2_nv_read_lock_cmd {
  141. TPMI_RH_NV_INDEX nvIndex;
  142. };
  143. struct tpm2_nv_write_lock_cmd {
  144. TPMI_RH_NV_INDEX nvIndex;
  145. };
  146. struct tpm2_nv_read_public_cmd {
  147. TPMI_RH_NV_INDEX nvIndex;
  148. };
  149. struct tpm2_hierarchy_control_cmd {
  150. TPMI_RH_ENABLES enable;
  151. TPMI_YES_NO state;
  152. };
  153. struct tpm2_get_capability_cmd {
  154. TPM_CAP capability;
  155. uint32_t property;
  156. uint32_t property_count;
  157. };
  158. struct tpm2_self_test_cmd {
  159. TPMI_YES_NO full_test;
  160. };
  161. struct tpm2_startup_cmd {
  162. TPM_SU startup_type;
  163. };
  164. struct tpm2_shutdown_cmd {
  165. TPM_SU shutdown_type;
  166. };
  167. /* Common command/response header. */
  168. struct tpm_header {
  169. uint16_t tpm_tag;
  170. uint32_t tpm_size;
  171. TPM_CC tpm_code;
  172. } __attribute__((packed));
  173. struct nv_read_response {
  174. uint32_t params_size;
  175. TPM2B_MAX_NV_BUFFER buffer;
  176. };
  177. struct tpm2_session_attrs {
  178. uint8_t continueSession : 1;
  179. uint8_t auditExclusive : 1;
  180. uint8_t auditReset : 1;
  181. uint8_t reserved3_4 : 2;
  182. uint8_t decrypt : 1;
  183. uint8_t encrypt : 1;
  184. uint8_t audit : 1;
  185. };
  186. struct tpm2_session_header {
  187. uint32_t session_handle;
  188. uint16_t nonce_size;
  189. uint8_t *nonce;
  190. union {
  191. struct tpm2_session_attrs session_attr_bits;
  192. uint8_t session_attrs;
  193. } __attribute__((packed));
  194. uint16_t auth_size;
  195. uint8_t *auth;
  196. };
  197. struct get_capability_response {
  198. TPMI_YES_NO more_data;
  199. TPMS_CAPABILITY_DATA capability_data;
  200. } __attribute__((packed));
  201. struct nv_read_public_response {
  202. TPMS_NV_PUBLIC nvPublic;
  203. TPM2B_NAME nvName;
  204. } __attribute__((packed));
  205. struct tpm2_response {
  206. struct tpm_header hdr;
  207. union {
  208. struct nv_read_response nvr;
  209. struct tpm2_session_header def_space;
  210. struct get_capability_response cap;
  211. struct nv_read_public_response nv_read_public;
  212. };
  213. };
  214. typedef struct {
  215. uint32_t ownerAuthSet : 1;
  216. uint32_t endorsementAuthSet : 1;
  217. uint32_t lockoutAuthSet : 1;
  218. uint32_t reserved3_7 : 5;
  219. uint32_t disableClear : 1;
  220. uint32_t inLockout : 1;
  221. uint32_t tpmGeneratedEPS : 1;
  222. uint32_t reserved11_31 : 21;
  223. } TPM_PERMANENT_FLAGS;
  224. typedef struct {
  225. uint32_t phEnable : 1;
  226. uint32_t shEnable : 1;
  227. uint32_t ehEnable : 1;
  228. uint32_t phEnableNV : 1;
  229. uint32_t reserved4_30 : 27;
  230. uint32_t orderly : 1;
  231. } TPM_STCLEAR_FLAGS;
  232. /* TODO(apronin): For TPM2 certain properties must be received using
  233. * TPM2_GetCapability instead of being hardcoded as they are now:
  234. * TPM_MAX_COMMAND_SIZE -> use TPM_PT_MAX_COMMAND_SIZE for TPM2.
  235. * TPM_PCR_DIGEST -> use TPM_PT_MAX_DIGEST for TPM2.
  236. */
  237. #define TPM_MAX_COMMAND_SIZE 4096
  238. #define TPM_PCR_DIGEST 32
  239. #endif /* ! __VBOOT_REFERENCE_FIRMWARE_INCLUDE_TPM2_TSS_CONSTANTS_H */