Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 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 | |
| 7 | #ifndef __SRC_LIB_TPM2_TLCL_STRUCTURES_H |
| 8 | #define __SRC_LIB_TPM2_TLCL_STRUCTURES_H |
| 9 | |
| 10 | /* |
| 11 | * This file includes a subset of definitions of TPM protocol version 2.x |
| 12 | * constants and structures needed for functions used in coreboot. |
| 13 | */ |
| 14 | #include <stdint.h> |
| 15 | #include <tpm_lite/tlcl.h> |
| 16 | #include <types.h> |
| 17 | |
| 18 | /* This should be plenty for what firmware needs. */ |
| 19 | #define TPM_BUFFER_SIZE 256 |
| 20 | |
| 21 | /* Basic TPM2 types. */ |
| 22 | typedef uint16_t TPM_SU; |
| 23 | typedef uint16_t TPM_ALG_ID; |
| 24 | typedef uint32_t TPM_CC; |
| 25 | typedef uint32_t TPM_HANDLE; |
| 26 | typedef uint32_t TPM_RC; |
| 27 | typedef uint8_t TPMI_YES_NO; |
| 28 | typedef TPM_HANDLE TPMI_RH_NV_INDEX; |
| 29 | typedef TPM_HANDLE TPMI_RH_PROVISION; |
| 30 | typedef TPM_HANDLE TPMI_SH_AUTH_SESSION; |
| 31 | typedef TPM_HANDLE TPM_RH; |
| 32 | typedef TPM_ALG_ID TPMI_ALG_HASH; |
| 33 | |
| 34 | /* Some hardcoded algorithm values. */ |
| 35 | #define TPM_ALG_HMAC ((TPM_ALG_ID)0x0005) |
| 36 | #define TPM_ALG_NULL ((TPM_ALG_ID)0x0010) |
| 37 | #define TPM_ALG_SHA1 ((TPM_ALG_ID)0x0004) |
| 38 | #define TPM_ALG_SHA256 ((TPM_ALG_ID)0x000b) |
| 39 | |
| 40 | /* Some hardcoded hierarchies. */ |
| 41 | #define TPM_RH_NULL 0x40000007 |
| 42 | #define TPM_RS_PW 0x40000009 |
| 43 | #define TPM_RH_PLATFORM 0x4000000C |
| 44 | |
| 45 | typedef struct { |
| 46 | uint16_t size; |
| 47 | uint8_t *buffer; |
| 48 | } TPM2B; |
| 49 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 50 | /* Relevant TPM Command's structures. */ |
| 51 | /* Common command/response header. */ |
| 52 | struct tpm_header { |
| 53 | uint16_t tpm_tag; |
| 54 | uint32_t tpm_size; |
| 55 | TPM_CC tpm_code; |
| 56 | } __attribute__((packed)); |
| 57 | |
| 58 | /* TPM command codes. */ |
Vadim Bendebury | 6acb9a6 | 2016-06-30 20:50:49 -0700 | [diff] [blame^] | 59 | #define TPM2_Clear ((TPM_CC)0x00000126) |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 60 | #define TPM2_NV_DefineSpace ((TPM_CC)0x0000012A) |
| 61 | #define TPM2_NV_Write ((TPM_CC)0x00000137) |
| 62 | #define TPM2_SelfTest ((TPM_CC)0x00000143) |
| 63 | #define TPM2_Startup ((TPM_CC)0x00000144) |
| 64 | #define TPM2_NV_Read ((TPM_CC)0x0000014E) |
| 65 | #define TPM2_GetCapability ((TPM_CC)0x0000017A) |
| 66 | |
| 67 | /* Startup values. */ |
| 68 | #define TPM_SU_CLEAR 0 |
| 69 | #define TPM_SU_STATE 1 |
| 70 | |
| 71 | #define TPM_HT_NV_INDEX 0x01 |
| 72 | #define TPM_HT_HMAC_SESSION 0x02 |
| 73 | #define TPM_HT_POLICY_SESSION 0x03 |
| 74 | |
| 75 | #define HR_SHIFT 24 |
| 76 | #define HR_PCR (TPM_HT_PCR << HR_SHIFT) |
| 77 | #define HR_HMAC_SESSION (TPM_HT_HMAC_SESSION << HR_SHIFT) |
| 78 | #define HR_POLICY_SESSION (TPM_HT_POLICY_SESSION << HR_SHIFT) |
| 79 | #define HR_TRANSIENT (TPM_HT_TRANSIENT << HR_SHIFT) |
| 80 | #define HR_PERSISTENT (TPM_HT_PERSISTENT << HR_SHIFT) |
| 81 | #define HR_NV_INDEX (TPM_HT_NV_INDEX << HR_SHIFT) |
| 82 | #define HR_PERMANENT (TPM_HT_PERMANENT << HR_SHIFT) |
| 83 | #define PCR_FIRST (HR_PCR + 0) |
| 84 | #define PCR_LAST (PCR_FIRST + IMPLEMENTATION_PCR-1) |
| 85 | #define HMAC_SESSION_FIRST (HR_HMAC_SESSION + 0) |
| 86 | #define HMAC_SESSION_LAST (HMAC_SESSION_FIRST+MAX_ACTIVE_SESSIONS-1) |
| 87 | #define LOADED_SESSION_FIRST HMAC_SESSION_FIRST |
| 88 | #define LOADED_SESSION_LAST HMAC_SESSION_LAST |
| 89 | #define POLICY_SESSION_FIRST (HR_POLICY_SESSION + 0) |
| 90 | #define POLICY_SESSION_LAST (POLICY_SESSION_FIRST + MAX_ACTIVE_SESSIONS-1) |
| 91 | #define TRANSIENT_FIRST (HR_TRANSIENT + 0) |
| 92 | #define ACTIVE_SESSION_FIRST POLICY_SESSION_FIRST |
| 93 | #define ACTIVE_SESSION_LAST POLICY_SESSION_LAST |
| 94 | #define TRANSIENT_LAST (TRANSIENT_FIRST+MAX_LOADED_OBJECTS-1) |
| 95 | #define PERSISTENT_FIRST (HR_PERSISTENT + 0) |
| 96 | #define PERSISTENT_LAST (PERSISTENT_FIRST + 0x00FFFFFF) |
| 97 | #define PLATFORM_PERSISTENT (PERSISTENT_FIRST + 0x00800000) |
| 98 | #define NV_INDEX_FIRST (HR_NV_INDEX + 0) |
| 99 | #define NV_INDEX_LAST (NV_INDEX_FIRST + 0x00FFFFFF) |
| 100 | #define PERMANENT_FIRST TPM_RH_FIRST |
| 101 | #define PERMANENT_LAST TPM_RH_LAST |
| 102 | |
| 103 | /* Tpm2 command tags. */ |
| 104 | #define TPM_ST_NO_SESSIONS 0x8001 |
| 105 | #define TPM_ST_SESSIONS 0x8002 |
| 106 | |
| 107 | #define RC_VER1 0x100 |
| 108 | #define TPM_RC_INITIALIZE ((TPM_RC)(RC_VER1 + 0x000)) |
| 109 | |
| 110 | /* TPM command structures. */ |
| 111 | |
| 112 | struct tpm2_startup { |
| 113 | TPM_SU startup_type; |
| 114 | }; |
| 115 | |
| 116 | /* Various TPM capability types to use when querying the device. */ |
| 117 | typedef uint32_t TPM_CAP; |
| 118 | #define TPM_CAP_TPM_PROPERTIES ((TPM_CAP)0x00000006) |
| 119 | |
| 120 | typedef TPM_HANDLE TPMI_RH_NV_AUTH; |
| 121 | typedef TPM_HANDLE TPMI_RH_NV_INDEX; |
| 122 | |
| 123 | /* TPM Property capability constants. */ |
| 124 | typedef uint32_t TPM_PT; |
| 125 | #define PT_GROUP 0x00000100 |
| 126 | #define PT_VAR (PT_GROUP * 2) |
| 127 | #define TPM_PT_PERMANENT ((TPM_PT)(PT_VAR + 0)) |
| 128 | |
| 129 | /* Structures of payloads of various TPM2 commands. */ |
| 130 | struct tpm2_get_capability { |
| 131 | TPM_CAP capability; |
| 132 | uint32_t property; |
| 133 | uint32_t propertyCount; |
| 134 | }; |
| 135 | |
| 136 | /* get_capability response when PT_PERMANENT is requested. */ |
| 137 | typedef struct { |
| 138 | uint32_t ownerAuthSet : 1; |
| 139 | uint32_t endorsementAuthSet : 1; |
| 140 | uint32_t lockoutAuthSet : 1; |
| 141 | uint32_t reserved3_7 : 5; |
| 142 | uint32_t disableClear : 1; |
| 143 | uint32_t inLockout : 1; |
| 144 | uint32_t tpmGeneratedEPS : 1; |
| 145 | uint32_t reserved11_31 : 21; |
| 146 | } TPMA_PERMANENT; |
| 147 | |
| 148 | typedef struct { |
| 149 | uint32_t TPMA_NV_PPWRITE : 1; |
| 150 | uint32_t TPMA_NV_OWNERWRITE : 1; |
| 151 | uint32_t TPMA_NV_AUTHWRITE : 1; |
| 152 | uint32_t TPMA_NV_POLICYWRITE : 1; |
| 153 | uint32_t TPMA_NV_COUNTER : 1; |
| 154 | uint32_t TPMA_NV_BITS : 1; |
| 155 | uint32_t TPMA_NV_EXTEND : 1; |
| 156 | uint32_t reserved7_9 : 3; |
| 157 | uint32_t TPMA_NV_POLICY_DELETE : 1; |
| 158 | uint32_t TPMA_NV_WRITELOCKED : 1; |
| 159 | uint32_t TPMA_NV_WRITEALL : 1; |
| 160 | uint32_t TPMA_NV_WRITEDEFINE : 1; |
| 161 | uint32_t TPMA_NV_WRITE_STCLEAR : 1; |
| 162 | uint32_t TPMA_NV_GLOBALLOCK : 1; |
| 163 | uint32_t TPMA_NV_PPREAD : 1; |
| 164 | uint32_t TPMA_NV_OWNERREAD : 1; |
| 165 | uint32_t TPMA_NV_AUTHREAD : 1; |
| 166 | uint32_t TPMA_NV_POLICYREAD : 1; |
| 167 | uint32_t reserved20_24 : 5; |
| 168 | uint32_t TPMA_NV_NO_DA : 1; |
| 169 | uint32_t TPMA_NV_ORDERLY : 1; |
| 170 | uint32_t TPMA_NV_CLEAR_STCLEAR : 1; |
| 171 | uint32_t TPMA_NV_READLOCKED : 1; |
| 172 | uint32_t TPMA_NV_WRITTEN : 1; |
| 173 | uint32_t TPMA_NV_PLATFORMCREATE : 1; |
| 174 | uint32_t TPMA_NV_READ_STCLEAR : 1; |
| 175 | } TPMA_NV; |
| 176 | |
| 177 | typedef union { |
| 178 | struct { |
| 179 | uint16_t size; |
Vadim Bendebury | 1a98050 | 2016-07-07 11:15:47 -0700 | [diff] [blame] | 180 | const uint8_t *buffer; |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 181 | } t; |
| 182 | TPM2B b; |
| 183 | } TPM2B_DIGEST; |
| 184 | |
| 185 | typedef TPM2B_DIGEST TPM2B_AUTH; |
| 186 | typedef TPM2B_DIGEST TPM2B_NONCE; |
| 187 | |
| 188 | typedef struct { |
| 189 | TPM_PT property; |
| 190 | uint32_t value; |
| 191 | } TPMS_TAGGED_PROPERTY; |
| 192 | |
| 193 | #define MAX_CAP_DATA (TPM_BUFFER_SIZE - sizeof(struct tpm_header) - \ |
| 194 | sizeof(TPMI_YES_NO) - sizeof(TPM_CAP) - sizeof(uint32_t)) |
| 195 | #define MAX_TPM_PROPERTIES (MAX_CAP_DATA/sizeof(TPMS_TAGGED_PROPERTY)) |
| 196 | |
| 197 | /* Somewhat arbitrary, leave enough room for command wrappers. */ |
| 198 | #define MAX_NV_BUFFER_SIZE (TPM_BUFFER_SIZE - sizeof(struct tpm_header) - 50) |
| 199 | |
| 200 | typedef struct { |
| 201 | uint32_t count; |
| 202 | TPMS_TAGGED_PROPERTY tpmProperty[MAX_TPM_PROPERTIES]; |
| 203 | } TPML_TAGGED_TPM_PROPERTY; |
| 204 | |
| 205 | typedef union { |
| 206 | TPML_TAGGED_TPM_PROPERTY tpmProperties; |
| 207 | } TPMU_CAPABILITIES; |
| 208 | |
| 209 | typedef struct { |
| 210 | TPM_CAP capability; |
| 211 | TPMU_CAPABILITIES data; |
| 212 | } TPMS_CAPABILITY_DATA; |
| 213 | |
| 214 | struct get_cap_response { |
| 215 | TPMI_YES_NO more_data; |
| 216 | TPMS_CAPABILITY_DATA cd; |
| 217 | }; |
| 218 | |
| 219 | typedef struct { |
| 220 | TPMI_RH_NV_INDEX nvIndex; |
| 221 | TPMI_ALG_HASH nameAlg; |
| 222 | TPMA_NV attributes; |
| 223 | TPM2B_DIGEST authPolicy; |
| 224 | uint16_t dataSize; |
| 225 | } TPMS_NV_PUBLIC; |
| 226 | |
| 227 | typedef union { |
| 228 | struct { |
| 229 | uint16_t size; |
| 230 | TPMS_NV_PUBLIC nvPublic; |
| 231 | } t; |
| 232 | TPM2B b; |
| 233 | } TPM2B_NV_PUBLIC; |
| 234 | |
| 235 | typedef union { |
| 236 | struct { |
| 237 | uint16_t size; |
| 238 | const uint8_t *buffer; |
| 239 | } t; |
| 240 | TPM2B b; |
| 241 | } TPM2B_MAX_NV_BUFFER; |
| 242 | |
| 243 | struct nv_read_response { |
| 244 | uint32_t params_size; |
| 245 | TPM2B_MAX_NV_BUFFER buffer; |
| 246 | }; |
| 247 | |
| 248 | struct tpm2_session_attrs { |
| 249 | uint8_t continueSession : 1; |
| 250 | uint8_t auditExclusive : 1; |
| 251 | uint8_t auditReset : 1; |
| 252 | uint8_t reserved3_4 : 2; |
| 253 | uint8_t decrypt : 1; |
| 254 | uint8_t encrypt : 1; |
| 255 | uint8_t audit : 1; |
| 256 | }; |
| 257 | |
| 258 | /* |
| 259 | * TPM session header for commands requiring session information. Also |
| 260 | * included in the responses to those commands. |
| 261 | */ |
| 262 | struct tpm2_session_header { |
| 263 | uint32_t session_handle; |
| 264 | uint16_t nonce_size; |
| 265 | uint8_t *nonce; |
| 266 | union { |
| 267 | struct tpm2_session_attrs session_attr_bits; |
| 268 | uint8_t session_attrs; |
| 269 | } __attribute__((packed)); |
| 270 | uint16_t auth_size; |
| 271 | uint8_t *auth; |
| 272 | }; |
| 273 | |
| 274 | struct tpm2_response { |
| 275 | struct tpm_header hdr; |
| 276 | union { |
| 277 | struct get_cap_response gc; |
| 278 | struct nv_read_response nvr; |
| 279 | struct tpm2_session_header def_space; |
| 280 | }; |
| 281 | }; |
| 282 | |
| 283 | struct tpm2_nv_define_space_cmd { |
| 284 | TPM2B_AUTH auth; |
| 285 | TPMS_NV_PUBLIC publicInfo; |
| 286 | }; |
| 287 | |
| 288 | struct tpm2_nv_write_cmd { |
| 289 | TPMI_RH_NV_INDEX nvIndex; |
| 290 | TPM2B_MAX_NV_BUFFER data; |
| 291 | uint16_t offset; |
| 292 | }; |
| 293 | |
| 294 | struct tpm2_self_test { |
| 295 | TPMI_YES_NO yes_no; |
| 296 | }; |
| 297 | |
| 298 | struct tpm2_nv_read_cmd { |
| 299 | TPMI_RH_NV_INDEX nvIndex; |
| 300 | uint16_t size; |
| 301 | uint16_t offset; |
| 302 | }; |
| 303 | |
| 304 | #endif // __SRC_LIB_TPM2_TLCL_STRUCTURES_H |