Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 3 | * Defines APIs and structures that allow software to interact with a |
| 4 | * TPM2 device |
| 5 | * |
| 6 | * Copyright (c) 2020 Linaro |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 7 | * Copyright (c) 2018 Bootlin |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 8 | * |
Heinrich Schuchardt | 4fc6509 | 2024-12-27 14:25:41 +0200 | [diff] [blame] | 9 | * The structures are described in |
| 10 | * Trusted Platform Module Library Part 2: Structures |
| 11 | * http://tcg.tjn.chef.causewaynow.com/resource/tpm-library-specification/ |
| 12 | * |
| 13 | * C header files are listed in |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 14 | * https://trustedcomputinggroup.org/resource/tss-overview-common-structures-specification/ |
| 15 | * |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 16 | * Author: Miquel Raynal <miquel.raynal@bootlin.com> |
| 17 | */ |
| 18 | |
| 19 | #ifndef __TPM_V2_H |
| 20 | #define __TPM_V2_H |
| 21 | |
| 22 | #include <tpm-common.h> |
| 23 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 24 | struct udevice; |
| 25 | |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 26 | #define TPM2_DIGEST_LEN 32 |
| 27 | |
Ilias Apalodimas | cae28ef | 2020-11-30 11:47:39 +0200 | [diff] [blame] | 28 | #define TPM2_SHA1_DIGEST_SIZE 20 |
| 29 | #define TPM2_SHA256_DIGEST_SIZE 32 |
| 30 | #define TPM2_SHA384_DIGEST_SIZE 48 |
| 31 | #define TPM2_SHA512_DIGEST_SIZE 64 |
| 32 | #define TPM2_SM3_256_DIGEST_SIZE 32 |
| 33 | |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 34 | #define TPM2_MAX_PCRS 32 |
| 35 | #define TPM2_PCR_SELECT_MAX ((TPM2_MAX_PCRS + 7) / 8) |
| 36 | #define TPM2_MAX_CAP_BUFFER 1024 |
| 37 | #define TPM2_MAX_TPM_PROPERTIES ((TPM2_MAX_CAP_BUFFER - sizeof(u32) /* TPM2_CAP */ - \ |
| 38 | sizeof(u32)) / sizeof(struct tpms_tagged_property)) |
| 39 | |
Simon Glass | ca31f07 | 2021-07-18 14:18:03 -0600 | [diff] [blame] | 40 | #define TPM2_HDR_LEN 10 |
| 41 | |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 42 | #define TPM2_CAP_PCRS 0x00000005U |
| 43 | #define TPM2_CAP_TPM_PROPERTIES 0x00000006U |
| 44 | |
| 45 | /* Definition of (UINT32) TPM2_PT Constants */ |
| 46 | #define TPM2_PT_GROUP (u32)(0x00000100) |
| 47 | #define TPM2_PT_FIXED (u32)(TPM2_PT_GROUP * 1) |
| 48 | #define TPM2_PT_MANUFACTURER (u32)(TPM2_PT_FIXED + 5) |
| 49 | #define TPM2_PT_PCR_COUNT (u32)(TPM2_PT_FIXED + 18) |
| 50 | #define TPM2_PT_MAX_COMMAND_SIZE (u32)(TPM2_PT_FIXED + 30) |
| 51 | #define TPM2_PT_MAX_RESPONSE_SIZE (u32)(TPM2_PT_FIXED + 31) |
| 52 | |
Heinrich Schuchardt | 4fc6509 | 2024-12-27 14:25:41 +0200 | [diff] [blame] | 53 | /** |
| 54 | * struct tpms_tagged_property - TPMS_TAGGED_PROPERTY structure |
| 55 | * |
| 56 | * This structure is returned by TPM2_GetCapability() to report |
| 57 | * a u32 property value. |
| 58 | * |
| 59 | * @property: property identifier |
| 60 | * @value: value of the property |
| 61 | */ |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 62 | struct tpms_tagged_property { |
| 63 | u32 property; |
| 64 | u32 value; |
| 65 | } __packed; |
| 66 | |
Heinrich Schuchardt | 4fc6509 | 2024-12-27 14:25:41 +0200 | [diff] [blame] | 67 | /** |
| 68 | * struct tpms_pcr_selection - TPMS_PCR_SELECTION structure |
| 69 | * |
| 70 | * This structure allows to specify a hash algorithm and a list of |
| 71 | * selected PCRs. A PCR is selected by setting the related bit in |
| 72 | * @pcr_select to 1. |
| 73 | * |
| 74 | * @hash: hash algorithm associated with the selection |
| 75 | * @size_of_select: size in bytes of the @pcr_select array |
| 76 | * @pcr_select: bit map of selected PCRs |
| 77 | */ |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 78 | struct tpms_pcr_selection { |
| 79 | u16 hash; |
| 80 | u8 size_of_select; |
| 81 | u8 pcr_select[TPM2_PCR_SELECT_MAX]; |
| 82 | } __packed; |
| 83 | |
Heinrich Schuchardt | 4fc6509 | 2024-12-27 14:25:41 +0200 | [diff] [blame] | 84 | /** |
| 85 | * struct tpml_pcr_selection - TPML_PCR_SELECTION structure |
| 86 | * |
| 87 | * @count: number of selection structures, may be zero |
| 88 | * @selection: list of selections |
| 89 | */ |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 90 | struct tpml_pcr_selection { |
| 91 | u32 count; |
| 92 | struct tpms_pcr_selection selection[TPM2_NUM_PCR_BANKS]; |
| 93 | } __packed; |
| 94 | |
| 95 | /* TPML_TAGGED_TPM_PROPERTY Structure */ |
| 96 | struct tpml_tagged_tpm_property { |
| 97 | u32 count; |
| 98 | struct tpms_tagged_property tpm_property[TPM2_MAX_TPM_PROPERTIES]; |
| 99 | } __packed; |
| 100 | |
| 101 | /* TPMU_CAPABILITIES Union */ |
| 102 | union tpmu_capabilities { |
| 103 | /* |
| 104 | * Non exhaustive. Only added the structs needed for our |
| 105 | * current code |
| 106 | */ |
| 107 | struct tpml_pcr_selection assigned_pcr; |
| 108 | struct tpml_tagged_tpm_property tpm_properties; |
| 109 | } __packed; |
| 110 | |
| 111 | /* TPMS_CAPABILITY_DATA Structure */ |
| 112 | struct tpms_capability_data { |
| 113 | u32 capability; |
| 114 | union tpmu_capabilities data; |
| 115 | } __packed; |
| 116 | |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 117 | /** |
Ilias Apalodimas | cae28ef | 2020-11-30 11:47:39 +0200 | [diff] [blame] | 118 | * Definition of TPMU_HA Union |
| 119 | */ |
Eddie James | 90b6c86 | 2023-10-24 10:43:47 -0500 | [diff] [blame] | 120 | union tpmu_ha { |
Ilias Apalodimas | cae28ef | 2020-11-30 11:47:39 +0200 | [diff] [blame] | 121 | u8 sha1[TPM2_SHA1_DIGEST_SIZE]; |
| 122 | u8 sha256[TPM2_SHA256_DIGEST_SIZE]; |
| 123 | u8 sm3_256[TPM2_SM3_256_DIGEST_SIZE]; |
| 124 | u8 sha384[TPM2_SHA384_DIGEST_SIZE]; |
| 125 | u8 sha512[TPM2_SHA512_DIGEST_SIZE]; |
| 126 | } __packed; |
| 127 | |
| 128 | /** |
| 129 | * Definition of TPMT_HA Structure |
| 130 | * |
| 131 | * @hash_alg: Hash algorithm defined in enum tpm2_algorithms |
| 132 | * @digest: Digest value for a given algorithm |
| 133 | */ |
| 134 | struct tpmt_ha { |
| 135 | u16 hash_alg; |
Eddie James | 90b6c86 | 2023-10-24 10:43:47 -0500 | [diff] [blame] | 136 | union tpmu_ha digest; |
Ilias Apalodimas | cae28ef | 2020-11-30 11:47:39 +0200 | [diff] [blame] | 137 | } __packed; |
| 138 | |
| 139 | /** |
| 140 | * Definition of TPML_DIGEST_VALUES Structure |
| 141 | * |
| 142 | * @count: Number of algorithms supported by hardware |
| 143 | * @digests: struct for algorithm id and hash value |
| 144 | */ |
| 145 | struct tpml_digest_values { |
| 146 | u32 count; |
| 147 | struct tpmt_ha digests[TPM2_NUM_PCR_BANKS]; |
| 148 | } __packed; |
| 149 | |
| 150 | /** |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 151 | * TPM2 Structure Tags for command/response buffers. |
| 152 | * |
| 153 | * @TPM2_ST_NO_SESSIONS: the command does not need an authentication. |
| 154 | * @TPM2_ST_SESSIONS: the command needs an authentication. |
| 155 | */ |
| 156 | enum tpm2_structures { |
| 157 | TPM2_ST_NO_SESSIONS = 0x8001, |
| 158 | TPM2_ST_SESSIONS = 0x8002, |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * TPM2 type of boolean. |
| 163 | */ |
| 164 | enum tpm2_yes_no { |
| 165 | TPMI_YES = 1, |
| 166 | TPMI_NO = 0, |
| 167 | }; |
| 168 | |
| 169 | /** |
| 170 | * TPM2 startup values. |
| 171 | * |
| 172 | * @TPM2_SU_CLEAR: reset the internal state. |
| 173 | * @TPM2_SU_STATE: restore saved state (if any). |
| 174 | */ |
| 175 | enum tpm2_startup_types { |
| 176 | TPM2_SU_CLEAR = 0x0000, |
| 177 | TPM2_SU_STATE = 0x0001, |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * TPM2 permanent handles. |
| 182 | * |
| 183 | * @TPM2_RH_OWNER: refers to the 'owner' hierarchy. |
| 184 | * @TPM2_RS_PW: indicates a password. |
| 185 | * @TPM2_RH_LOCKOUT: refers to the 'lockout' hierarchy. |
| 186 | * @TPM2_RH_ENDORSEMENT: refers to the 'endorsement' hierarchy. |
| 187 | * @TPM2_RH_PLATFORM: refers to the 'platform' hierarchy. |
| 188 | */ |
| 189 | enum tpm2_handles { |
| 190 | TPM2_RH_OWNER = 0x40000001, |
| 191 | TPM2_RS_PW = 0x40000009, |
| 192 | TPM2_RH_LOCKOUT = 0x4000000A, |
| 193 | TPM2_RH_ENDORSEMENT = 0x4000000B, |
| 194 | TPM2_RH_PLATFORM = 0x4000000C, |
| 195 | }; |
| 196 | |
| 197 | /** |
| 198 | * TPM2 command codes used at the beginning of a buffer, gives the command. |
| 199 | * |
| 200 | * @TPM2_CC_STARTUP: TPM2_Startup(). |
| 201 | * @TPM2_CC_SELF_TEST: TPM2_SelfTest(). |
| 202 | * @TPM2_CC_CLEAR: TPM2_Clear(). |
| 203 | * @TPM2_CC_CLEARCONTROL: TPM2_ClearControl(). |
| 204 | * @TPM2_CC_HIERCHANGEAUTH: TPM2_HierarchyChangeAuth(). |
| 205 | * @TPM2_CC_PCR_SETAUTHPOL: TPM2_PCR_SetAuthPolicy(). |
| 206 | * @TPM2_CC_DAM_RESET: TPM2_DictionaryAttackLockReset(). |
| 207 | * @TPM2_CC_DAM_PARAMETERS: TPM2_DictionaryAttackParameters(). |
| 208 | * @TPM2_CC_GET_CAPABILITY: TPM2_GetCapibility(). |
Dhananjay Phadke | 7a2cf2e | 2020-06-04 16:43:59 -0700 | [diff] [blame] | 209 | * @TPM2_CC_GET_RANDOM: TPM2_GetRandom(). |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 210 | * @TPM2_CC_PCR_READ: TPM2_PCR_Read(). |
| 211 | * @TPM2_CC_PCR_EXTEND: TPM2_PCR_Extend(). |
| 212 | * @TPM2_CC_PCR_SETAUTHVAL: TPM2_PCR_SetAuthValue(). |
| 213 | */ |
| 214 | enum tpm2_command_codes { |
| 215 | TPM2_CC_STARTUP = 0x0144, |
| 216 | TPM2_CC_SELF_TEST = 0x0143, |
Simon Glass | 77759db | 2021-02-06 14:23:42 -0700 | [diff] [blame] | 217 | TPM2_CC_HIER_CONTROL = 0x0121, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 218 | TPM2_CC_CLEAR = 0x0126, |
| 219 | TPM2_CC_CLEARCONTROL = 0x0127, |
| 220 | TPM2_CC_HIERCHANGEAUTH = 0x0129, |
Simon Glass | 713c58a | 2021-02-06 14:23:39 -0700 | [diff] [blame] | 221 | TPM2_CC_NV_DEFINE_SPACE = 0x012a, |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 222 | TPM2_CC_PCR_SETAUTHPOL = 0x012C, |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 223 | TPM2_CC_NV_WRITE = 0x0137, |
Simon Glass | e9d3d59 | 2021-02-06 14:23:41 -0700 | [diff] [blame] | 224 | TPM2_CC_NV_WRITELOCK = 0x0138, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 225 | TPM2_CC_DAM_RESET = 0x0139, |
| 226 | TPM2_CC_DAM_PARAMETERS = 0x013A, |
Simon Glass | 5ff3f16 | 2018-10-01 11:55:17 -0600 | [diff] [blame] | 227 | TPM2_CC_NV_READ = 0x014E, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 228 | TPM2_CC_GET_CAPABILITY = 0x017A, |
Dhananjay Phadke | 7a2cf2e | 2020-06-04 16:43:59 -0700 | [diff] [blame] | 229 | TPM2_CC_GET_RANDOM = 0x017B, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 230 | TPM2_CC_PCR_READ = 0x017E, |
| 231 | TPM2_CC_PCR_EXTEND = 0x0182, |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 232 | TPM2_CC_PCR_SETAUTHVAL = 0x0183, |
Raymond Mao | f69f2d7 | 2025-01-27 06:58:47 -0800 | [diff] [blame] | 233 | TPM2_CC_PCR_ALLOCATE = 0x012B, |
Raymond Mao | 5187a64 | 2025-01-27 06:58:46 -0800 | [diff] [blame] | 234 | TPM2_CC_SHUTDOWN = 0x0145, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | /** |
| 238 | * TPM2 return codes. |
| 239 | */ |
| 240 | enum tpm2_return_codes { |
| 241 | TPM2_RC_SUCCESS = 0x0000, |
| 242 | TPM2_RC_BAD_TAG = 0x001E, |
| 243 | TPM2_RC_FMT1 = 0x0080, |
| 244 | TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003, |
| 245 | TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004, |
| 246 | TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015, |
| 247 | TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022, |
| 248 | TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B, |
| 249 | TPM2_RC_VER1 = 0x0100, |
| 250 | TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000, |
| 251 | TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001, |
| 252 | TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020, |
| 253 | TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025, |
| 254 | TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043, |
| 255 | TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044, |
| 256 | TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045, |
Simon Glass | 77759db | 2021-02-06 14:23:42 -0700 | [diff] [blame] | 257 | TPM2_RC_NV_DEFINED = TPM2_RC_VER1 + 0x004c, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 258 | TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053, |
| 259 | TPM2_RC_WARN = 0x0900, |
| 260 | TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A, |
| 261 | TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010, |
| 262 | TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021, |
| 263 | }; |
| 264 | |
| 265 | /** |
| 266 | * TPM2 algorithms. |
| 267 | */ |
| 268 | enum tpm2_algorithms { |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 269 | TPM2_ALG_SHA1 = 0x04, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 270 | TPM2_ALG_XOR = 0x0A, |
| 271 | TPM2_ALG_SHA256 = 0x0B, |
| 272 | TPM2_ALG_SHA384 = 0x0C, |
| 273 | TPM2_ALG_SHA512 = 0x0D, |
| 274 | TPM2_ALG_NULL = 0x10, |
Ilias Apalodimas | f4e0590 | 2020-11-11 11:18:10 +0200 | [diff] [blame] | 275 | TPM2_ALG_SM3_256 = 0x12, |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 278 | /** |
| 279 | * struct digest_info - details of supported digests |
| 280 | * |
| 281 | * @hash_name: hash name |
| 282 | * @hash_alg: hash algorithm id |
| 283 | * @hash_mask: hash registry mask |
| 284 | * @hash_len: hash digest length |
| 285 | */ |
| 286 | struct digest_info { |
| 287 | const char *hash_name; |
| 288 | u16 hash_alg; |
| 289 | u32 hash_mask; |
| 290 | u16 hash_len; |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 291 | bool supported; |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | /* Algorithm Registry */ |
| 295 | #define TCG2_BOOT_HASH_ALG_SHA1 0x00000001 |
| 296 | #define TCG2_BOOT_HASH_ALG_SHA256 0x00000002 |
| 297 | #define TCG2_BOOT_HASH_ALG_SHA384 0x00000004 |
| 298 | #define TCG2_BOOT_HASH_ALG_SHA512 0x00000008 |
| 299 | #define TCG2_BOOT_HASH_ALG_SM3_256 0x00000010 |
| 300 | |
| 301 | static const struct digest_info hash_algo_list[] = { |
| 302 | { |
| 303 | "sha1", |
| 304 | TPM2_ALG_SHA1, |
| 305 | TCG2_BOOT_HASH_ALG_SHA1, |
| 306 | TPM2_SHA1_DIGEST_SIZE, |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 307 | #if IS_ENABLED(CONFIG_SHA1) |
| 308 | true, |
| 309 | #else |
| 310 | false, |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 311 | #endif |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 312 | }, |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 313 | { |
| 314 | "sha256", |
| 315 | TPM2_ALG_SHA256, |
| 316 | TCG2_BOOT_HASH_ALG_SHA256, |
| 317 | TPM2_SHA256_DIGEST_SIZE, |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 318 | #if IS_ENABLED(CONFIG_SHA256) |
| 319 | true, |
| 320 | #else |
| 321 | false, |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 322 | #endif |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 323 | }, |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 324 | { |
| 325 | "sha384", |
| 326 | TPM2_ALG_SHA384, |
| 327 | TCG2_BOOT_HASH_ALG_SHA384, |
| 328 | TPM2_SHA384_DIGEST_SIZE, |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 329 | #if IS_ENABLED(CONFIG_SHA384) |
| 330 | true, |
| 331 | #else |
| 332 | false, |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 333 | #endif |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 334 | }, |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 335 | { |
| 336 | "sha512", |
| 337 | TPM2_ALG_SHA512, |
| 338 | TCG2_BOOT_HASH_ALG_SHA512, |
| 339 | TPM2_SHA512_DIGEST_SIZE, |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 340 | #if IS_ENABLED(CONFIG_SHA512) |
| 341 | true, |
| 342 | #else |
| 343 | false, |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 344 | #endif |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 345 | }, |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 346 | }; |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 347 | |
Simon Glass | b4ebd1f | 2018-11-23 21:29:34 -0700 | [diff] [blame] | 348 | /* NV index attributes */ |
| 349 | enum tpm_index_attrs { |
| 350 | TPMA_NV_PPWRITE = 1UL << 0, |
| 351 | TPMA_NV_OWNERWRITE = 1UL << 1, |
| 352 | TPMA_NV_AUTHWRITE = 1UL << 2, |
| 353 | TPMA_NV_POLICYWRITE = 1UL << 3, |
| 354 | TPMA_NV_COUNTER = 1UL << 4, |
| 355 | TPMA_NV_BITS = 1UL << 5, |
| 356 | TPMA_NV_EXTEND = 1UL << 6, |
| 357 | TPMA_NV_POLICY_DELETE = 1UL << 10, |
| 358 | TPMA_NV_WRITELOCKED = 1UL << 11, |
| 359 | TPMA_NV_WRITEALL = 1UL << 12, |
| 360 | TPMA_NV_WRITEDEFINE = 1UL << 13, |
| 361 | TPMA_NV_WRITE_STCLEAR = 1UL << 14, |
| 362 | TPMA_NV_GLOBALLOCK = 1UL << 15, |
| 363 | TPMA_NV_PPREAD = 1UL << 16, |
| 364 | TPMA_NV_OWNERREAD = 1UL << 17, |
| 365 | TPMA_NV_AUTHREAD = 1UL << 18, |
| 366 | TPMA_NV_POLICYREAD = 1UL << 19, |
| 367 | TPMA_NV_NO_DA = 1UL << 25, |
| 368 | TPMA_NV_ORDERLY = 1UL << 26, |
| 369 | TPMA_NV_CLEAR_STCLEAR = 1UL << 27, |
| 370 | TPMA_NV_READLOCKED = 1UL << 28, |
| 371 | TPMA_NV_WRITTEN = 1UL << 29, |
| 372 | TPMA_NV_PLATFORMCREATE = 1UL << 30, |
| 373 | TPMA_NV_READ_STCLEAR = 1UL << 31, |
| 374 | |
| 375 | TPMA_NV_MASK_READ = TPMA_NV_PPREAD | TPMA_NV_OWNERREAD | |
| 376 | TPMA_NV_AUTHREAD | TPMA_NV_POLICYREAD, |
| 377 | TPMA_NV_MASK_WRITE = TPMA_NV_PPWRITE | TPMA_NV_OWNERWRITE | |
| 378 | TPMA_NV_AUTHWRITE | TPMA_NV_POLICYWRITE, |
| 379 | }; |
| 380 | |
Simon Glass | e1ed0ec | 2020-02-06 09:55:03 -0700 | [diff] [blame] | 381 | enum { |
| 382 | TPM_ACCESS_VALID = 1 << 7, |
| 383 | TPM_ACCESS_ACTIVE_LOCALITY = 1 << 5, |
| 384 | TPM_ACCESS_REQUEST_PENDING = 1 << 2, |
| 385 | TPM_ACCESS_REQUEST_USE = 1 << 1, |
| 386 | TPM_ACCESS_ESTABLISHMENT = 1 << 0, |
| 387 | }; |
| 388 | |
| 389 | enum { |
| 390 | TPM_STS_FAMILY_SHIFT = 26, |
| 391 | TPM_STS_FAMILY_MASK = 0x3 << TPM_STS_FAMILY_SHIFT, |
| 392 | TPM_STS_FAMILY_TPM2 = 1 << TPM_STS_FAMILY_SHIFT, |
| 393 | TPM_STS_RESE_TESTABLISMENT_BIT = 1 << 25, |
| 394 | TPM_STS_COMMAND_CANCEL = 1 << 24, |
| 395 | TPM_STS_BURST_COUNT_SHIFT = 8, |
| 396 | TPM_STS_BURST_COUNT_MASK = 0xffff << TPM_STS_BURST_COUNT_SHIFT, |
| 397 | TPM_STS_VALID = 1 << 7, |
| 398 | TPM_STS_COMMAND_READY = 1 << 6, |
| 399 | TPM_STS_GO = 1 << 5, |
| 400 | TPM_STS_DATA_AVAIL = 1 << 4, |
| 401 | TPM_STS_DATA_EXPECT = 1 << 3, |
| 402 | TPM_STS_SELF_TEST_DONE = 1 << 2, |
| 403 | TPM_STS_RESPONSE_RETRY = 1 << 1, |
Ilias Apalodimas | 97f5e2d | 2021-11-09 09:02:17 +0200 | [diff] [blame] | 404 | TPM_STS_READ_ZERO = 0x23 |
Simon Glass | e1ed0ec | 2020-02-06 09:55:03 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | enum { |
| 408 | TPM_CMD_COUNT_OFFSET = 2, |
| 409 | TPM_CMD_ORDINAL_OFFSET = 6, |
| 410 | TPM_MAX_BUF_SIZE = 1260, |
| 411 | }; |
| 412 | |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 413 | enum { |
| 414 | /* Secure storage for firmware settings */ |
| 415 | TPM_HT_PCR = 0, |
| 416 | TPM_HT_NV_INDEX, |
| 417 | TPM_HT_HMAC_SESSION, |
| 418 | TPM_HT_POLICY_SESSION, |
| 419 | |
| 420 | HR_SHIFT = 24, |
| 421 | HR_PCR = TPM_HT_PCR << HR_SHIFT, |
| 422 | HR_HMAC_SESSION = TPM_HT_HMAC_SESSION << HR_SHIFT, |
| 423 | HR_POLICY_SESSION = TPM_HT_POLICY_SESSION << HR_SHIFT, |
| 424 | HR_NV_INDEX = TPM_HT_NV_INDEX << HR_SHIFT, |
| 425 | }; |
| 426 | |
Miquel Raynal | 65a1a6c | 2018-05-15 11:57:12 +0200 | [diff] [blame] | 427 | /** |
| 428 | * Issue a TPM2_Startup command. |
| 429 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 430 | * @dev TPM device |
Miquel Raynal | 65a1a6c | 2018-05-15 11:57:12 +0200 | [diff] [blame] | 431 | * @mode TPM startup mode |
| 432 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 433 | * Return: code of the operation |
Miquel Raynal | 65a1a6c | 2018-05-15 11:57:12 +0200 | [diff] [blame] | 434 | */ |
Raymond Mao | 5187a64 | 2025-01-27 06:58:46 -0800 | [diff] [blame] | 435 | u32 tpm2_startup(struct udevice *dev, bool onoff, enum tpm2_startup_types mode); |
Miquel Raynal | 65a1a6c | 2018-05-15 11:57:12 +0200 | [diff] [blame] | 436 | |
Miquel Raynal | 39c7608 | 2018-05-15 11:57:13 +0200 | [diff] [blame] | 437 | /** |
| 438 | * Issue a TPM2_SelfTest command. |
| 439 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 440 | * @dev TPM device |
Miquel Raynal | 39c7608 | 2018-05-15 11:57:13 +0200 | [diff] [blame] | 441 | * @full_test Asking to perform all tests or only the untested ones |
| 442 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 443 | * Return: code of the operation |
Miquel Raynal | 39c7608 | 2018-05-15 11:57:13 +0200 | [diff] [blame] | 444 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 445 | u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test); |
Miquel Raynal | 39c7608 | 2018-05-15 11:57:13 +0200 | [diff] [blame] | 446 | |
Miquel Raynal | 8df6f8d | 2018-05-15 11:57:14 +0200 | [diff] [blame] | 447 | /** |
| 448 | * Issue a TPM2_Clear command. |
| 449 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 450 | * @dev TPM device |
Miquel Raynal | 8df6f8d | 2018-05-15 11:57:14 +0200 | [diff] [blame] | 451 | * @handle Handle |
| 452 | * @pw Password |
| 453 | * @pw_sz Length of the password |
| 454 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 455 | * Return: code of the operation |
Miquel Raynal | 8df6f8d | 2018-05-15 11:57:14 +0200 | [diff] [blame] | 456 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 457 | u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw, |
| 458 | const ssize_t pw_sz); |
Miquel Raynal | 8df6f8d | 2018-05-15 11:57:14 +0200 | [diff] [blame] | 459 | |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 460 | /** |
Simon Glass | 713c58a | 2021-02-06 14:23:39 -0700 | [diff] [blame] | 461 | * Issue a TPM_NV_DefineSpace command |
| 462 | * |
| 463 | * This allows a space to be defined with given attributes and policy |
| 464 | * |
| 465 | * @dev TPM device |
| 466 | * @space_index index of the area |
| 467 | * @space_size size of area in bytes |
| 468 | * @nv_attributes TPM_NV_ATTRIBUTES of the area |
| 469 | * @nv_policy policy to use |
| 470 | * @nv_policy_size size of the policy |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 471 | * Return: return code of the operation |
Simon Glass | 713c58a | 2021-02-06 14:23:39 -0700 | [diff] [blame] | 472 | */ |
| 473 | u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index, |
| 474 | size_t space_size, u32 nv_attributes, |
| 475 | const u8 *nv_policy, size_t nv_policy_size); |
| 476 | |
| 477 | /** |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 478 | * Issue a TPM2_PCR_Extend command. |
| 479 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 480 | * @dev TPM device |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 481 | * @index Index of the PCR |
Ilias Apalodimas | 7f59c71 | 2020-11-26 23:07:22 +0200 | [diff] [blame] | 482 | * @algorithm Algorithm used, defined in 'enum tpm2_algorithms' |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 483 | * @digest Value representing the event to be recorded |
Ilias Apalodimas | 7f59c71 | 2020-11-26 23:07:22 +0200 | [diff] [blame] | 484 | * @digest_len len of the hash |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 485 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 486 | * Return: code of the operation |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 487 | */ |
Ilias Apalodimas | 7f59c71 | 2020-11-26 23:07:22 +0200 | [diff] [blame] | 488 | u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm, |
| 489 | const u8 *digest, u32 digest_len); |
Miquel Raynal | 14d7235 | 2018-05-15 11:57:15 +0200 | [diff] [blame] | 490 | |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 491 | /** |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 492 | * Read data from the secure storage |
| 493 | * |
| 494 | * @dev TPM device |
| 495 | * @index Index of data to read |
| 496 | * @data Place to put data |
| 497 | * @count Number of bytes of data |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 498 | * Return: code of the operation |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 499 | */ |
| 500 | u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count); |
| 501 | |
| 502 | /** |
| 503 | * Write data to the secure storage |
| 504 | * |
| 505 | * @dev TPM device |
| 506 | * @index Index of data to write |
| 507 | * @data Data to write |
| 508 | * @count Number of bytes of data |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 509 | * Return: code of the operation |
Simon Glass | 3d930ed | 2021-02-06 14:23:40 -0700 | [diff] [blame] | 510 | */ |
| 511 | u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data, |
| 512 | u32 count); |
| 513 | |
| 514 | /** |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 515 | * Issue a TPM2_PCR_Read command. |
| 516 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 517 | * @dev TPM device |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 518 | * @idx Index of the PCR |
| 519 | * @idx_min_sz Minimum size in bytes of the pcrSelect array |
Ruchika Gupta | 686bedb | 2021-11-29 13:09:45 +0530 | [diff] [blame] | 520 | * @algorithm Algorithm used, defined in 'enum tpm2_algorithms' |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 521 | * @data Output buffer for contents of the named PCR |
Ruchika Gupta | 686bedb | 2021-11-29 13:09:45 +0530 | [diff] [blame] | 522 | * @digest_len len of the data |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 523 | * @updates Optional out parameter: number of updates for this PCR |
| 524 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 525 | * Return: code of the operation |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 526 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 527 | u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz, |
Ruchika Gupta | 686bedb | 2021-11-29 13:09:45 +0530 | [diff] [blame] | 528 | u16 algorithm, void *data, u32 digest_len, |
| 529 | unsigned int *updates); |
Miquel Raynal | 4c1a585 | 2018-05-15 11:57:16 +0200 | [diff] [blame] | 530 | |
Miquel Raynal | 2e52c06 | 2018-05-15 11:57:17 +0200 | [diff] [blame] | 531 | /** |
| 532 | * Issue a TPM2_GetCapability command. This implementation is limited |
| 533 | * to query property index that is 4-byte wide. |
| 534 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 535 | * @dev TPM device |
Miquel Raynal | 2e52c06 | 2018-05-15 11:57:17 +0200 | [diff] [blame] | 536 | * @capability Partition of capabilities |
| 537 | * @property Further definition of capability, limited to be 4 bytes wide |
| 538 | * @buf Output buffer for capability information |
| 539 | * @prop_count Size of output buffer |
| 540 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 541 | * Return: code of the operation |
Miquel Raynal | 2e52c06 | 2018-05-15 11:57:17 +0200 | [diff] [blame] | 542 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 543 | u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property, |
| 544 | void *buf, size_t prop_count); |
Miquel Raynal | 2e52c06 | 2018-05-15 11:57:17 +0200 | [diff] [blame] | 545 | |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 546 | /** |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 547 | * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks |
| 548 | * |
| 549 | * @dev: TPM device |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 550 | * @pcrs: struct tpml_pcr_selection of available PCRs |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 551 | * |
| 552 | * @return 0 on success, code of operation or negative errno on failure |
| 553 | */ |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 554 | int tpm2_get_pcr_info(struct udevice *dev, struct tpml_pcr_selection *pcrs); |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 555 | |
| 556 | /** |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 557 | * Issue a TPM2_DictionaryAttackLockReset command. |
| 558 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 559 | * @dev TPM device |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 560 | * @pw Password |
| 561 | * @pw_sz Length of the password |
| 562 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 563 | * Return: code of the operation |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 564 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 565 | u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz); |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 566 | |
| 567 | /** |
| 568 | * Issue a TPM2_DictionaryAttackParameters command. |
| 569 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 570 | * @dev TPM device |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 571 | * @pw Password |
| 572 | * @pw_sz Length of the password |
| 573 | * @max_tries Count of authorizations before lockout |
| 574 | * @recovery_time Time before decrementation of the failure count |
| 575 | * @lockout_recovery Time to wait after a lockout |
| 576 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 577 | * Return: code of the operation |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 578 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 579 | u32 tpm2_dam_parameters(struct udevice *dev, const char *pw, |
| 580 | const ssize_t pw_sz, unsigned int max_tries, |
| 581 | unsigned int recovery_time, |
Miquel Raynal | 228e990 | 2018-05-15 11:57:18 +0200 | [diff] [blame] | 582 | unsigned int lockout_recovery); |
| 583 | |
Miquel Raynal | 05d7be3 | 2018-05-15 11:57:19 +0200 | [diff] [blame] | 584 | /** |
| 585 | * Issue a TPM2_HierarchyChangeAuth command. |
| 586 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 587 | * @dev TPM device |
Miquel Raynal | 05d7be3 | 2018-05-15 11:57:19 +0200 | [diff] [blame] | 588 | * @handle Handle |
| 589 | * @newpw New password |
| 590 | * @newpw_sz Length of the new password |
| 591 | * @oldpw Old password |
| 592 | * @oldpw_sz Length of the old password |
| 593 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 594 | * Return: code of the operation |
Miquel Raynal | 05d7be3 | 2018-05-15 11:57:19 +0200 | [diff] [blame] | 595 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 596 | int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw, |
| 597 | const ssize_t newpw_sz, const char *oldpw, |
| 598 | const ssize_t oldpw_sz); |
Miquel Raynal | 05d7be3 | 2018-05-15 11:57:19 +0200 | [diff] [blame] | 599 | |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 600 | /** |
| 601 | * Issue a TPM_PCR_SetAuthPolicy command. |
| 602 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 603 | * @dev TPM device |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 604 | * @pw Platform password |
| 605 | * @pw_sz Length of the password |
| 606 | * @index Index of the PCR |
| 607 | * @digest New key to access the PCR |
| 608 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 609 | * Return: code of the operation |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 610 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 611 | u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw, |
| 612 | const ssize_t pw_sz, u32 index, const char *key); |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 613 | |
| 614 | /** |
| 615 | * Issue a TPM_PCR_SetAuthValue command. |
| 616 | * |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 617 | * @dev TPM device |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 618 | * @pw Platform password |
| 619 | * @pw_sz Length of the password |
| 620 | * @index Index of the PCR |
| 621 | * @digest New key to access the PCR |
| 622 | * @key_sz Length of the new key |
| 623 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 624 | * Return: code of the operation |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 625 | */ |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 626 | u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw, |
| 627 | const ssize_t pw_sz, u32 index, const char *key, |
| 628 | const ssize_t key_sz); |
Miquel Raynal | 0b864f6 | 2018-05-15 11:57:20 +0200 | [diff] [blame] | 629 | |
Dhananjay Phadke | 7a2cf2e | 2020-06-04 16:43:59 -0700 | [diff] [blame] | 630 | /** |
| 631 | * Issue a TPM2_GetRandom command. |
| 632 | * |
| 633 | * @dev TPM device |
| 634 | * @param data output buffer for the random bytes |
| 635 | * @param count size of output buffer |
| 636 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 637 | * Return: return code of the operation |
Dhananjay Phadke | 7a2cf2e | 2020-06-04 16:43:59 -0700 | [diff] [blame] | 638 | */ |
| 639 | u32 tpm2_get_random(struct udevice *dev, void *data, u32 count); |
| 640 | |
Simon Glass | e9d3d59 | 2021-02-06 14:23:41 -0700 | [diff] [blame] | 641 | /** |
| 642 | * Lock data in the TPM |
| 643 | * |
| 644 | * Once locked the data cannot be written until after a reboot |
| 645 | * |
| 646 | * @dev TPM device |
| 647 | * @index Index of data to lock |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 648 | * Return: code of the operation |
Simon Glass | e9d3d59 | 2021-02-06 14:23:41 -0700 | [diff] [blame] | 649 | */ |
| 650 | u32 tpm2_write_lock(struct udevice *dev, u32 index); |
| 651 | |
Simon Glass | 77759db | 2021-02-06 14:23:42 -0700 | [diff] [blame] | 652 | /** |
| 653 | * Disable access to any platform data |
| 654 | * |
| 655 | * This can be called to close off access to the firmware data in the data, |
| 656 | * before calling the kernel. |
| 657 | * |
| 658 | * @dev TPM device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 659 | * Return: code of the operation |
Simon Glass | 77759db | 2021-02-06 14:23:42 -0700 | [diff] [blame] | 660 | */ |
| 661 | u32 tpm2_disable_platform_hierarchy(struct udevice *dev); |
| 662 | |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 663 | /** |
| 664 | * submit user specified data to the TPM and get response |
| 665 | * |
| 666 | * @dev TPM device |
| 667 | * @sendbuf: Buffer of the data to send |
| 668 | * @recvbuf: Buffer to save the response to |
| 669 | * @recv_size: Pointer to the size of the response buffer |
| 670 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 671 | * Return: code of the operation |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 672 | */ |
| 673 | u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf, |
| 674 | u8 *recvbuf, size_t *recv_size); |
| 675 | |
Simon Glass | 3f7a73a | 2022-08-30 21:05:37 -0600 | [diff] [blame] | 676 | /** |
| 677 | * tpm_cr50_report_state() - Report the Cr50 internal state |
| 678 | * |
| 679 | * @dev: TPM device |
| 680 | * @vendor_cmd: Vendor command number to send |
| 681 | * @vendor_subcmd: Vendor sub-command number to send |
| 682 | * @recvbuf: Buffer to save the response to |
| 683 | * @recv_size: Pointer to the size of the response buffer |
| 684 | * Return: result of the operation |
| 685 | */ |
| 686 | u32 tpm2_report_state(struct udevice *dev, uint vendor_cmd, uint vendor_subcmd, |
| 687 | u8 *recvbuf, size_t *recv_size); |
| 688 | |
Simon Glass | 3564b8e | 2022-08-30 21:05:38 -0600 | [diff] [blame] | 689 | /** |
| 690 | * tpm2_enable_nvcommits() - Tell TPM to commit NV data immediately |
| 691 | * |
| 692 | * For Chromium OS verified boot, we may reboot or reset at different times, |
| 693 | * possibly leaving non-volatile data unwritten by the TPM. |
| 694 | * |
| 695 | * This vendor command is used to indicate that non-volatile data should be |
| 696 | * written to its store immediately. |
| 697 | * |
| 698 | * @dev TPM device |
| 699 | * @vendor_cmd: Vendor command number to send |
| 700 | * @vendor_subcmd: Vendor sub-command number to send |
| 701 | * Return: result of the operation |
| 702 | */ |
| 703 | u32 tpm2_enable_nvcommits(struct udevice *dev, uint vendor_cmd, |
| 704 | uint vendor_subcmd); |
| 705 | |
Ilias Apalodimas | 42d7bdf | 2023-01-25 12:18:36 +0200 | [diff] [blame] | 706 | /** |
Raymond Mao | f0c9125 | 2025-01-27 06:58:48 -0800 | [diff] [blame] | 707 | * tpm2_scan_masks - Scan the bitmask of algorithms based on the |
| 708 | * active/supported banks and the one from eventlog. |
| 709 | * |
| 710 | * @dev TPM device |
| 711 | * @log_active Active algorithm bitmask |
| 712 | * @mask Bitmask to set |
| 713 | * |
| 714 | * Return: zero on success, negative errno otherwise |
| 715 | */ |
| 716 | int tpm2_scan_masks(struct udevice *dev, u32 log_active, u32 *mask); |
| 717 | |
| 718 | /** |
Raymond Mao | f69f2d7 | 2025-01-27 06:58:47 -0800 | [diff] [blame] | 719 | * tpm2_pcr_config_algo() - Allocate the active PCRs. Requires reboot |
| 720 | * |
| 721 | * @dev TPM device |
| 722 | * @algo_mask Mask of the algorithms |
| 723 | * @pcr PCR structure for allocation |
| 724 | * @pcr_len Actual PCR data length |
| 725 | * |
| 726 | * Return: code of the operation |
| 727 | */ |
| 728 | u32 tpm2_pcr_config_algo(struct udevice *dev, u32 algo_mask, |
| 729 | struct tpml_pcr_selection *pcr, u32 *pcr_len); |
| 730 | |
| 731 | /** |
| 732 | * tpm2_send_pcr_allocate() - Send PCR allocate command. Requires reboot |
| 733 | * |
| 734 | * @dev TPM device |
| 735 | * @pw Platform password |
| 736 | * @pw_sz Length of the password |
| 737 | * @pcr PCR structure for allocation |
| 738 | * @pcr_len Actual PCR data length |
| 739 | * |
| 740 | * Return: code of the operation |
| 741 | */ |
| 742 | u32 tpm2_send_pcr_allocate(struct udevice *dev, const char *pw, |
| 743 | const ssize_t pw_sz, struct tpml_pcr_selection *pcr, |
| 744 | u32 pcr_len); |
Raymond Mao | f0c9125 | 2025-01-27 06:58:48 -0800 | [diff] [blame] | 745 | /** |
| 746 | * tpm2_activate_banks() - Activate PCR banks |
| 747 | * |
| 748 | * @param dev TPM device |
| 749 | * @log_active Bitmask of eventlog algorithms |
| 750 | * |
| 751 | * Return: code of the operation |
| 752 | */ |
| 753 | int tpm2_activate_banks(struct udevice *dev, u32 log_active); |
Raymond Mao | f69f2d7 | 2025-01-27 06:58:47 -0800 | [diff] [blame] | 754 | |
| 755 | /** |
Ilias Apalodimas | 42d7bdf | 2023-01-25 12:18:36 +0200 | [diff] [blame] | 756 | * tpm2_auto_start() - start up the TPM and perform selftests. |
| 757 | * If a testable function has not been tested and is |
| 758 | * requested the TPM2 will return TPM_RC_NEEDS_TEST. |
| 759 | * |
| 760 | * @param dev TPM device |
| 761 | * Return: TPM2_RC_TESTING, if TPM2 self-test is in progress. |
| 762 | * TPM2_RC_SUCCESS, if testing of all functions is complete without |
| 763 | * functional failures. |
| 764 | * TPM2_RC_FAILURE, if any test failed. |
| 765 | * TPM2_RC_INITIALIZE, if the TPM has not gone through the Startup |
| 766 | * sequence |
| 767 | |
| 768 | */ |
| 769 | u32 tpm2_auto_start(struct udevice *dev); |
| 770 | |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 771 | /** |
| 772 | * tpm2_name_to_algorithm() - Return an algorithm id given a supported |
| 773 | * algorithm name |
| 774 | * |
| 775 | * @name: algorithm name |
| 776 | * Return: enum tpm2_algorithms or -EINVAL |
| 777 | */ |
| 778 | enum tpm2_algorithms tpm2_name_to_algorithm(const char *name); |
| 779 | |
| 780 | /** |
| 781 | * tpm2_algorithm_name() - Return an algorithm name string for a |
| 782 | * supported algorithm id |
| 783 | * |
| 784 | * @algorithm_id: algorithm defined in enum tpm2_algorithms |
| 785 | * Return: algorithm name string or "" |
| 786 | */ |
| 787 | const char *tpm2_algorithm_name(enum tpm2_algorithms); |
| 788 | |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 789 | /** |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 790 | * tpm2_algorithm_supported() - Check if the algorithm supported by U-Boot |
| 791 | * |
| 792 | * @algorithm_id: algorithm defined in enum tpm2_algorithms |
| 793 | * Return: true if supported, otherwise false |
| 794 | */ |
| 795 | bool tpm2_algorithm_supported(enum tpm2_algorithms algo); |
| 796 | |
| 797 | /** |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 798 | * tpm2_algorithm_to_len() - Return an algorithm length for supported algorithm id |
| 799 | * |
| 800 | * @algorithm_id: algorithm defined in enum tpm2_algorithms |
| 801 | * Return: len or 0 if not supported |
| 802 | */ |
| 803 | u16 tpm2_algorithm_to_len(enum tpm2_algorithms algo); |
| 804 | |
| 805 | /* |
| 806 | * When measured boot is enabled via EFI or bootX commands all the algorithms |
| 807 | * above are selected by our Kconfigs. Due to U-Boots nature of being small there |
| 808 | * are cases where we need some functionality from the TPM -- e.g storage or RNG |
| 809 | * but we don't want to support measurements. |
| 810 | * |
| 811 | * The choice of hash algorithms are determined by the platform and the TPM |
| 812 | * configuration. Failing to cap a PCR in a bank which the platform left |
| 813 | * active is a security vulnerability. It permits the unsealing of secrets |
| 814 | * if an attacker can replay a good set of measurements into an unused bank. |
| 815 | * |
| 816 | * On top of that a previous stage bootloader (e.g TF-A), migh pass an eventlog |
| 817 | * since it doesn't have a TPM driver, which U-Boot needs to replace. The algorit h |
| 818 | * choice is a compile time option in that case and we need to make sure we conform. |
| 819 | * |
| 820 | * Add a variable here that sums the supported algorithms U-Boot was compiled |
| 821 | * with so we can refuse to do measurements if we don't support all of them |
| 822 | */ |
| 823 | |
| 824 | /** |
Ilias Apalodimas | d788b06 | 2024-12-24 08:01:05 -0800 | [diff] [blame] | 825 | * tpm2_check_active_banks() - Check if the active PCR banks are supported by |
| 826 | * our configuration |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 827 | * |
| 828 | * @dev: TPM device |
| 829 | * Return: true if allowed |
| 830 | */ |
Ilias Apalodimas | d788b06 | 2024-12-24 08:01:05 -0800 | [diff] [blame] | 831 | bool tpm2_check_active_banks(struct udevice *dev); |
Ilias Apalodimas | 1e665f9 | 2024-06-23 14:48:18 +0300 | [diff] [blame] | 832 | |
| 833 | /** |
Ilias Apalodimas | 9465f7a | 2024-12-24 08:01:04 -0800 | [diff] [blame] | 834 | * tpm2_is_active_bank() - check the pcr_select. If at least one of the PCRs |
| 835 | * supports the algorithm add it on the active ones |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 836 | * |
| 837 | * @selection: PCR selection structure |
| 838 | * Return: True if the algorithm is active |
| 839 | */ |
Ilias Apalodimas | 9465f7a | 2024-12-24 08:01:04 -0800 | [diff] [blame] | 840 | bool tpm2_is_active_bank(struct tpms_pcr_selection *selection); |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 841 | |
Ilias Apalodimas | 7b1e522 | 2024-12-24 08:01:08 -0800 | [diff] [blame] | 842 | /** |
| 843 | * tpm2_print_active_banks() - Print the active TPM PCRs |
| 844 | * |
| 845 | * @dev: TPM device |
| 846 | */ |
| 847 | void tpm2_print_active_banks(struct udevice *dev); |
| 848 | |
Miquel Raynal | f3b4350 | 2018-05-15 11:57:08 +0200 | [diff] [blame] | 849 | #endif /* __TPM_V2_H */ |