Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2023 Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #include <dm.h> |
| 7 | #include <dm/of_access.h> |
| 8 | #include <tpm_api.h> |
| 9 | #include <tpm-common.h> |
| 10 | #include <tpm-v2.h> |
| 11 | #include <tpm_tcg2.h> |
| 12 | #include <u-boot/sha1.h> |
| 13 | #include <u-boot/sha256.h> |
| 14 | #include <u-boot/sha512.h> |
| 15 | #include <version_string.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <linux/unaligned/be_byteshift.h> |
| 19 | #include <linux/unaligned/generic.h> |
| 20 | #include <linux/unaligned/le_byteshift.h> |
| 21 | #include "tpm-utils.h" |
| 22 | |
Raymond Mao | 6434c4a | 2024-12-24 08:01:06 -0800 | [diff] [blame] | 23 | int tcg2_get_pcr_info(struct udevice *dev, u32 *supported_bank, u32 *active_bank, |
| 24 | u32 *bank_num) |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 25 | { |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 26 | struct tpml_pcr_selection pcrs; |
| 27 | size_t i; |
| 28 | u32 ret; |
| 29 | |
Raymond Mao | 6434c4a | 2024-12-24 08:01:06 -0800 | [diff] [blame] | 30 | *supported_bank = 0; |
| 31 | *active_bank = 0; |
| 32 | *bank_num = 0; |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 33 | |
| 34 | ret = tpm2_get_pcr_info(dev, &pcrs); |
| 35 | if (ret) |
| 36 | return ret; |
| 37 | |
| 38 | for (i = 0; i < pcrs.count; i++) { |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 39 | struct tpms_pcr_selection *sel = &pcrs.selection[i]; |
| 40 | u32 hash_mask = tcg2_algorithm_to_mask(sel->hash); |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 41 | |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 42 | if (tpm2_algorithm_supported(sel->hash)) |
Raymond Mao | 6434c4a | 2024-12-24 08:01:06 -0800 | [diff] [blame] | 43 | *supported_bank |= hash_mask; |
Raymond Mao | 4315812 | 2024-12-24 08:01:07 -0800 | [diff] [blame] | 44 | else |
| 45 | log_warning("%s: unknown algorithm %x\n", __func__, |
| 46 | sel->hash); |
| 47 | |
| 48 | if (tpm2_is_active_bank(sel)) |
| 49 | *active_bank |= hash_mask; |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 50 | } |
| 51 | |
Raymond Mao | 6434c4a | 2024-12-24 08:01:06 -0800 | [diff] [blame] | 52 | *bank_num = pcrs.count; |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 57 | int tcg2_get_active_pcr_banks(struct udevice *dev, u32 *active_pcr_banks) |
| 58 | { |
| 59 | u32 supported = 0; |
| 60 | u32 pcr_banks = 0; |
| 61 | u32 active = 0; |
| 62 | int rc; |
| 63 | |
Ilias Apalodimas | cb35661 | 2024-06-23 14:48:17 +0300 | [diff] [blame] | 64 | rc = tcg2_get_pcr_info(dev, &supported, &active, &pcr_banks); |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 65 | if (rc) |
| 66 | return rc; |
| 67 | |
| 68 | *active_pcr_banks = active; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | u32 tcg2_event_get_size(struct tpml_digest_values *digest_list) |
| 74 | { |
| 75 | u32 len; |
| 76 | size_t i; |
| 77 | |
| 78 | len = offsetof(struct tcg_pcr_event2, digests); |
| 79 | len += offsetof(struct tpml_digest_values, digests); |
| 80 | for (i = 0; i < digest_list->count; ++i) { |
| 81 | u16 l = tpm2_algorithm_to_len(digest_list->digests[i].hash_alg); |
| 82 | |
| 83 | if (!l) |
| 84 | continue; |
| 85 | |
| 86 | len += l + offsetof(struct tpmt_ha, digest); |
| 87 | } |
| 88 | len += sizeof(u32); |
| 89 | |
| 90 | return len; |
| 91 | } |
| 92 | |
| 93 | int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length, |
| 94 | struct tpml_digest_values *digest_list) |
| 95 | { |
Ilias Apalodimas | e5ade05 | 2024-12-24 08:01:10 -0800 | [diff] [blame] | 96 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 97 | u8 final[sizeof(union tpmu_ha)]; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 98 | #if IS_ENABLED(CONFIG_SHA256) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 99 | sha256_context ctx_256; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 100 | #endif |
| 101 | #if IS_ENABLED(CONFIG_SHA512) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 102 | sha512_context ctx_512; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 103 | #endif |
| 104 | #if IS_ENABLED(CONFIG_SHA1) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 105 | sha1_context ctx; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 106 | #endif |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 107 | size_t i; |
| 108 | u32 len; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 109 | |
| 110 | digest_list->count = 0; |
Ilias Apalodimas | e5ade05 | 2024-12-24 08:01:10 -0800 | [diff] [blame] | 111 | for (i = 0; i < priv->active_bank_count; i++) { |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 112 | |
Ilias Apalodimas | e5ade05 | 2024-12-24 08:01:10 -0800 | [diff] [blame] | 113 | switch (priv->active_banks[i]) { |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 114 | #if IS_ENABLED(CONFIG_SHA1) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 115 | case TPM2_ALG_SHA1: |
| 116 | sha1_starts(&ctx); |
| 117 | sha1_update(&ctx, input, length); |
| 118 | sha1_finish(&ctx, final); |
| 119 | len = TPM2_SHA1_DIGEST_SIZE; |
| 120 | break; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 121 | #endif |
| 122 | #if IS_ENABLED(CONFIG_SHA256) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 123 | case TPM2_ALG_SHA256: |
| 124 | sha256_starts(&ctx_256); |
| 125 | sha256_update(&ctx_256, input, length); |
| 126 | sha256_finish(&ctx_256, final); |
| 127 | len = TPM2_SHA256_DIGEST_SIZE; |
| 128 | break; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 129 | #endif |
| 130 | #if IS_ENABLED(CONFIG_SHA384) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 131 | case TPM2_ALG_SHA384: |
| 132 | sha384_starts(&ctx_512); |
| 133 | sha384_update(&ctx_512, input, length); |
| 134 | sha384_finish(&ctx_512, final); |
| 135 | len = TPM2_SHA384_DIGEST_SIZE; |
| 136 | break; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 137 | #endif |
| 138 | #if IS_ENABLED(CONFIG_SHA512) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 139 | case TPM2_ALG_SHA512: |
| 140 | sha512_starts(&ctx_512); |
| 141 | sha512_update(&ctx_512, input, length); |
| 142 | sha512_finish(&ctx_512, final); |
| 143 | len = TPM2_SHA512_DIGEST_SIZE; |
| 144 | break; |
Raymond Mao | 5b5de6b | 2024-12-24 08:01:13 -0800 | [diff] [blame] | 145 | #endif |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 146 | default: |
| 147 | printf("%s: unsupported algorithm %x\n", __func__, |
Ilias Apalodimas | e5ade05 | 2024-12-24 08:01:10 -0800 | [diff] [blame] | 148 | priv->active_banks[i]); |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 149 | continue; |
| 150 | } |
| 151 | |
| 152 | digest_list->digests[digest_list->count].hash_alg = |
Ilias Apalodimas | e5ade05 | 2024-12-24 08:01:10 -0800 | [diff] [blame] | 153 | priv->active_banks[i]; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 154 | memcpy(&digest_list->digests[digest_list->count].digest, final, |
| 155 | len); |
| 156 | digest_list->count++; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | void tcg2_log_append(u32 pcr_index, u32 event_type, |
| 163 | struct tpml_digest_values *digest_list, u32 size, |
| 164 | const u8 *event, u8 *log) |
| 165 | { |
| 166 | size_t len; |
| 167 | size_t pos; |
| 168 | u32 i; |
| 169 | |
| 170 | pos = offsetof(struct tcg_pcr_event2, pcr_index); |
| 171 | put_unaligned_le32(pcr_index, log); |
| 172 | pos = offsetof(struct tcg_pcr_event2, event_type); |
| 173 | put_unaligned_le32(event_type, log + pos); |
| 174 | pos = offsetof(struct tcg_pcr_event2, digests) + |
| 175 | offsetof(struct tpml_digest_values, count); |
| 176 | put_unaligned_le32(digest_list->count, log + pos); |
| 177 | |
| 178 | pos = offsetof(struct tcg_pcr_event2, digests) + |
| 179 | offsetof(struct tpml_digest_values, digests); |
| 180 | for (i = 0; i < digest_list->count; ++i) { |
| 181 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 182 | |
| 183 | len = tpm2_algorithm_to_len(hash_alg); |
| 184 | if (!len) |
| 185 | continue; |
| 186 | |
| 187 | pos += offsetof(struct tpmt_ha, hash_alg); |
| 188 | put_unaligned_le16(hash_alg, log + pos); |
| 189 | pos += offsetof(struct tpmt_ha, digest); |
| 190 | memcpy(log + pos, (u8 *)&digest_list->digests[i].digest, len); |
| 191 | pos += len; |
| 192 | } |
| 193 | |
| 194 | put_unaligned_le32(size, log + pos); |
| 195 | pos += sizeof(u32); |
| 196 | memcpy(log + pos, event, size); |
| 197 | } |
| 198 | |
| 199 | static int tcg2_log_append_check(struct tcg2_event_log *elog, u32 pcr_index, |
| 200 | u32 event_type, |
| 201 | struct tpml_digest_values *digest_list, |
| 202 | u32 size, const u8 *event) |
| 203 | { |
| 204 | u32 event_size; |
| 205 | u8 *log; |
| 206 | |
| 207 | event_size = size + tcg2_event_get_size(digest_list); |
| 208 | if (elog->log_position + event_size > elog->log_size) { |
| 209 | printf("%s: log too large: %u + %u > %u\n", __func__, |
| 210 | elog->log_position, event_size, elog->log_size); |
| 211 | return -ENOBUFS; |
| 212 | } |
| 213 | |
| 214 | log = elog->log + elog->log_position; |
| 215 | elog->log_position += event_size; |
| 216 | |
| 217 | tcg2_log_append(pcr_index, event_type, digest_list, size, event, log); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static int tcg2_log_init(struct udevice *dev, struct tcg2_event_log *elog) |
| 223 | { |
Ilias Apalodimas | c22a064 | 2024-12-24 08:01:11 -0800 | [diff] [blame] | 224 | struct tpm_chip_priv *priv = dev_get_uclass_priv(dev); |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 225 | struct tcg_efi_spec_id_event *ev; |
| 226 | struct tcg_pcr_event *log; |
| 227 | u32 event_size; |
| 228 | u32 count = 0; |
| 229 | u32 log_size; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 230 | size_t i; |
| 231 | u16 len; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 232 | |
Ilias Apalodimas | c22a064 | 2024-12-24 08:01:11 -0800 | [diff] [blame] | 233 | count = priv->active_bank_count; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 234 | event_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes); |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 235 | event_size += 1 + |
| 236 | (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count); |
| 237 | log_size = offsetof(struct tcg_pcr_event, event) + event_size; |
| 238 | |
| 239 | if (log_size > elog->log_size) { |
| 240 | printf("%s: log too large: %u > %u\n", __func__, log_size, |
| 241 | elog->log_size); |
| 242 | return -ENOBUFS; |
| 243 | } |
| 244 | |
| 245 | log = (struct tcg_pcr_event *)elog->log; |
| 246 | put_unaligned_le32(0, &log->pcr_index); |
| 247 | put_unaligned_le32(EV_NO_ACTION, &log->event_type); |
| 248 | memset(&log->digest, 0, sizeof(log->digest)); |
| 249 | put_unaligned_le32(event_size, &log->event_size); |
| 250 | |
| 251 | ev = (struct tcg_efi_spec_id_event *)log->event; |
| 252 | strlcpy((char *)ev->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, |
| 253 | sizeof(ev->signature)); |
| 254 | put_unaligned_le32(0, &ev->platform_class); |
| 255 | ev->spec_version_minor = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2; |
| 256 | ev->spec_version_major = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2; |
| 257 | ev->spec_errata = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2; |
| 258 | ev->uintn_size = sizeof(size_t) / sizeof(u32); |
| 259 | put_unaligned_le32(count, &ev->number_of_algorithms); |
| 260 | |
Ilias Apalodimas | c22a064 | 2024-12-24 08:01:11 -0800 | [diff] [blame] | 261 | for (i = 0; i < count; ++i) { |
| 262 | len = tpm2_algorithm_to_len(priv->active_banks[i]); |
| 263 | put_unaligned_le16(priv->active_banks[i], |
| 264 | &ev->digest_sizes[i].algorithm_id); |
| 265 | put_unaligned_le16(len, &ev->digest_sizes[i].digest_size); |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | *((u8 *)ev + (event_size - 1)) = 0; |
| 269 | elog->log_position = log_size; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int tcg2_replay_eventlog(struct tcg2_event_log *elog, |
| 275 | struct udevice *dev, |
| 276 | struct tpml_digest_values *digest_list, |
| 277 | u32 log_position) |
| 278 | { |
| 279 | const u32 offset = offsetof(struct tcg_pcr_event2, digests) + |
| 280 | offsetof(struct tpml_digest_values, digests); |
| 281 | u32 event_size; |
| 282 | u32 count; |
| 283 | u16 algo; |
| 284 | u32 pcr; |
| 285 | u32 pos; |
| 286 | u16 len; |
| 287 | u8 *log; |
| 288 | int rc; |
| 289 | u32 i; |
| 290 | |
| 291 | while (log_position + offset < elog->log_size) { |
| 292 | log = elog->log + log_position; |
| 293 | |
| 294 | pos = offsetof(struct tcg_pcr_event2, pcr_index); |
| 295 | pcr = get_unaligned_le32(log + pos); |
| 296 | pos = offsetof(struct tcg_pcr_event2, event_type); |
| 297 | if (!get_unaligned_le32(log + pos)) |
| 298 | return 0; |
| 299 | |
| 300 | pos = offsetof(struct tcg_pcr_event2, digests) + |
| 301 | offsetof(struct tpml_digest_values, count); |
| 302 | count = get_unaligned_le32(log + pos); |
| 303 | if (count > ARRAY_SIZE(hash_algo_list) || |
| 304 | (digest_list->count && digest_list->count != count)) |
| 305 | return 0; |
| 306 | |
| 307 | pos = offsetof(struct tcg_pcr_event2, digests) + |
| 308 | offsetof(struct tpml_digest_values, digests); |
| 309 | for (i = 0; i < count; ++i) { |
| 310 | pos += offsetof(struct tpmt_ha, hash_alg); |
| 311 | if (log_position + pos + sizeof(u16) >= elog->log_size) |
| 312 | return 0; |
| 313 | |
| 314 | algo = get_unaligned_le16(log + pos); |
| 315 | pos += offsetof(struct tpmt_ha, digest); |
| 316 | switch (algo) { |
| 317 | case TPM2_ALG_SHA1: |
| 318 | case TPM2_ALG_SHA256: |
| 319 | case TPM2_ALG_SHA384: |
| 320 | case TPM2_ALG_SHA512: |
| 321 | len = tpm2_algorithm_to_len(algo); |
| 322 | break; |
| 323 | default: |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | if (digest_list->count) { |
| 328 | if (algo != digest_list->digests[i].hash_alg || |
| 329 | log_position + pos + len >= elog->log_size) |
| 330 | return 0; |
| 331 | |
| 332 | memcpy(digest_list->digests[i].digest.sha512, |
| 333 | log + pos, len); |
| 334 | } |
| 335 | |
| 336 | pos += len; |
| 337 | } |
| 338 | |
| 339 | if (log_position + pos + sizeof(u32) >= elog->log_size) |
| 340 | return 0; |
| 341 | |
| 342 | event_size = get_unaligned_le32(log + pos); |
| 343 | pos += event_size + sizeof(u32); |
| 344 | if (log_position + pos > elog->log_size) |
| 345 | return 0; |
| 346 | |
| 347 | if (digest_list->count) { |
| 348 | rc = tcg2_pcr_extend(dev, pcr, digest_list); |
| 349 | if (rc) |
| 350 | return rc; |
| 351 | } |
| 352 | |
| 353 | log_position += pos; |
| 354 | } |
| 355 | |
| 356 | elog->log_position = log_position; |
| 357 | elog->found = true; |
| 358 | return 0; |
| 359 | } |
| 360 | |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 361 | static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog, |
| 362 | u32 *log_active) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 363 | { |
| 364 | struct tpml_digest_values digest_list; |
| 365 | struct tcg_efi_spec_id_event *event; |
| 366 | struct tcg_pcr_event *log; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 367 | u32 calc_size; |
| 368 | u32 active; |
| 369 | u32 count; |
| 370 | u32 evsz; |
| 371 | u32 mask; |
| 372 | u16 algo; |
| 373 | u16 len; |
| 374 | int rc; |
| 375 | u32 i; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 376 | |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 377 | *log_active = 0; |
| 378 | |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 379 | if (elog->log_size <= offsetof(struct tcg_pcr_event, event)) |
| 380 | return 0; |
| 381 | |
| 382 | log = (struct tcg_pcr_event *)elog->log; |
| 383 | if (get_unaligned_le32(&log->pcr_index) != 0 || |
| 384 | get_unaligned_le32(&log->event_type) != EV_NO_ACTION) |
| 385 | return 0; |
| 386 | |
| 387 | for (i = 0; i < sizeof(log->digest); i++) { |
| 388 | if (log->digest[i]) |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | evsz = get_unaligned_le32(&log->event_size); |
| 393 | if (evsz < offsetof(struct tcg_efi_spec_id_event, digest_sizes) || |
| 394 | evsz + offsetof(struct tcg_pcr_event, event) > elog->log_size) |
| 395 | return 0; |
| 396 | |
| 397 | event = (struct tcg_efi_spec_id_event *)log->event; |
| 398 | if (memcmp(event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, |
| 399 | sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03))) |
| 400 | return 0; |
| 401 | |
| 402 | if (event->spec_version_minor != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 || |
| 403 | event->spec_version_major != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2) |
| 404 | return 0; |
| 405 | |
| 406 | count = get_unaligned_le32(&event->number_of_algorithms); |
| 407 | if (count > ARRAY_SIZE(hash_algo_list)) |
| 408 | return 0; |
| 409 | |
| 410 | calc_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes) + |
| 411 | (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count) + |
| 412 | 1; |
| 413 | if (evsz != calc_size) |
| 414 | return 0; |
| 415 | |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 416 | /* |
| 417 | * Go through the algorithms the EventLog contains. If the EventLog |
| 418 | * algorithms don't match the active TPM ones exit and report the |
| 419 | * erroneous banks. |
| 420 | * We've already checked that U-Boot supports all the enabled TPM |
| 421 | * algorithms, so just check the EvenLog against the TPM active ones. |
| 422 | */ |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 423 | digest_list.count = 0; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 424 | for (i = 0; i < count; ++i) { |
| 425 | algo = get_unaligned_le16(&event->digest_sizes[i].algorithm_id); |
| 426 | mask = tcg2_algorithm_to_mask(algo); |
| 427 | |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 428 | switch (algo) { |
| 429 | case TPM2_ALG_SHA1: |
| 430 | case TPM2_ALG_SHA256: |
| 431 | case TPM2_ALG_SHA384: |
| 432 | case TPM2_ALG_SHA512: |
| 433 | len = get_unaligned_le16(&event->digest_sizes[i].digest_size); |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 434 | if (tpm2_algorithm_to_len(algo) != len) { |
| 435 | log_err("EventLog invalid algorithm length\n"); |
| 436 | return -1; |
| 437 | } |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 438 | digest_list.digests[digest_list.count++].hash_alg = algo; |
| 439 | break; |
| 440 | default: |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 441 | /* |
| 442 | * We can ignore this if the TPM PCRs is not extended |
| 443 | * by the previous bootloader. But for now just exit |
| 444 | */ |
| 445 | log_err("EventLog has unsupported algorithm 0x%x\n", |
| 446 | algo); |
| 447 | return -1; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 448 | } |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 449 | *log_active |= mask; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 450 | } |
| 451 | |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 452 | rc = tcg2_get_active_pcr_banks(dev, &active); |
| 453 | if (rc) |
| 454 | return rc; |
| 455 | /* If the EventLog and active algorithms don't match exit */ |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 456 | if (*log_active != active) |
| 457 | return -ERESTARTSYS; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 458 | |
| 459 | /* Read PCR0 to check if previous firmware extended the PCRs or not. */ |
| 460 | rc = tcg2_pcr_read(dev, 0, &digest_list); |
| 461 | if (rc) |
| 462 | return rc; |
| 463 | |
| 464 | for (i = 0; i < digest_list.count; ++i) { |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 465 | u8 hash_buf[TPM2_SHA512_DIGEST_SIZE] = { 0 }; |
| 466 | u16 hash_alg = digest_list.digests[i].hash_alg; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 467 | |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 468 | if (memcmp((u8 *)&digest_list.digests[i].digest, hash_buf, |
| 469 | tpm2_algorithm_to_len(hash_alg))) |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 470 | digest_list.count = 0; |
Ilias Apalodimas | 8b0cba8 | 2024-12-24 08:01:12 -0800 | [diff] [blame] | 471 | |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | return tcg2_replay_eventlog(elog, dev, &digest_list, |
| 475 | offsetof(struct tcg_pcr_event, event) + |
| 476 | evsz); |
| 477 | } |
| 478 | |
| 479 | int tcg2_pcr_extend(struct udevice *dev, u32 pcr_index, |
| 480 | struct tpml_digest_values *digest_list) |
| 481 | { |
| 482 | u32 rc; |
| 483 | u32 i; |
| 484 | |
| 485 | for (i = 0; i < digest_list->count; i++) { |
| 486 | u32 alg = digest_list->digests[i].hash_alg; |
| 487 | |
| 488 | rc = tpm2_pcr_extend(dev, pcr_index, alg, |
| 489 | (u8 *)&digest_list->digests[i].digest, |
| 490 | tpm2_algorithm_to_len(alg)); |
| 491 | if (rc) { |
| 492 | printf("%s: error pcr:%u alg:%08x\n", __func__, |
| 493 | pcr_index, alg); |
| 494 | return rc; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | int tcg2_pcr_read(struct udevice *dev, u32 pcr_index, |
| 502 | struct tpml_digest_values *digest_list) |
| 503 | { |
| 504 | struct tpm_chip_priv *priv; |
| 505 | u32 rc; |
| 506 | u32 i; |
| 507 | |
| 508 | priv = dev_get_uclass_priv(dev); |
| 509 | if (!priv) |
| 510 | return -ENODEV; |
| 511 | |
| 512 | for (i = 0; i < digest_list->count; i++) { |
| 513 | u32 alg = digest_list->digests[i].hash_alg; |
| 514 | u8 *digest = (u8 *)&digest_list->digests[i].digest; |
| 515 | |
| 516 | rc = tpm2_pcr_read(dev, pcr_index, priv->pcr_select_min, alg, |
| 517 | digest, tpm2_algorithm_to_len(alg), NULL); |
| 518 | if (rc) { |
| 519 | printf("%s: error pcr:%u alg:%08x\n", __func__, |
| 520 | pcr_index, alg); |
| 521 | return rc; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int tcg2_measure_data(struct udevice *dev, struct tcg2_event_log *elog, |
| 529 | u32 pcr_index, u32 size, const u8 *data, u32 event_type, |
| 530 | u32 event_size, const u8 *event) |
| 531 | { |
| 532 | struct tpml_digest_values digest_list; |
| 533 | int rc; |
| 534 | |
| 535 | if (data) |
| 536 | rc = tcg2_create_digest(dev, data, size, &digest_list); |
| 537 | else |
| 538 | rc = tcg2_create_digest(dev, event, event_size, &digest_list); |
| 539 | if (rc) |
| 540 | return rc; |
| 541 | |
| 542 | rc = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 543 | if (rc) |
| 544 | return rc; |
| 545 | |
| 546 | return tcg2_log_append_check(elog, pcr_index, event_type, &digest_list, |
| 547 | event_size, event); |
| 548 | } |
| 549 | |
| 550 | int tcg2_log_prepare_buffer(struct udevice *dev, struct tcg2_event_log *elog, |
| 551 | bool ignore_existing_log) |
| 552 | { |
| 553 | struct tcg2_event_log log; |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 554 | int rc; |
| 555 | u32 log_active = 0; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 556 | |
| 557 | elog->log_position = 0; |
| 558 | elog->found = false; |
| 559 | |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 560 | rc = tcg2_platform_get_log(dev, (void **)&log.log, &log.log_size); |
| 561 | if (!rc) { |
| 562 | log.log_position = 0; |
| 563 | log.found = false; |
| 564 | |
| 565 | if (!ignore_existing_log) { |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 566 | rc = tcg2_log_parse(dev, &log, &log_active); |
| 567 | if (rc == -ERESTARTSYS && log_active) |
| 568 | goto pcr_allocate; |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 569 | if (rc) |
| 570 | return rc; |
| 571 | } |
| 572 | |
| 573 | if (elog->log_size) { |
| 574 | if (log.found) { |
| 575 | if (elog->log_size < log.log_position) |
| 576 | return -ENOBUFS; |
| 577 | |
| 578 | /* |
| 579 | * Copy the discovered log into the user buffer |
| 580 | * if there's enough space. |
| 581 | */ |
| 582 | memcpy(elog->log, log.log, log.log_position); |
| 583 | } |
| 584 | |
| 585 | unmap_physmem(log.log, MAP_NOCACHE); |
| 586 | } else { |
| 587 | elog->log = log.log; |
| 588 | elog->log_size = log.log_size; |
| 589 | } |
| 590 | |
| 591 | elog->log_position = log.log_position; |
| 592 | elog->found = log.found; |
| 593 | } |
| 594 | |
Raymond Mao | 631bca0 | 2025-01-27 06:58:49 -0800 | [diff] [blame] | 595 | pcr_allocate: |
| 596 | rc = tpm2_activate_banks(dev, log_active); |
| 597 | if (rc) |
| 598 | return rc; |
| 599 | |
Ilias Apalodimas | 95537a6 | 2024-06-23 14:48:15 +0300 | [diff] [blame] | 600 | /* |
| 601 | * Initialize the log buffer if no log was discovered and the buffer is |
| 602 | * valid. User's can pass in their own buffer as a fallback if no |
| 603 | * memory region is found. |
| 604 | */ |
| 605 | if (!elog->found && elog->log_size) |
| 606 | rc = tcg2_log_init(dev, elog); |
| 607 | |
| 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | int tcg2_measurement_init(struct udevice **dev, struct tcg2_event_log *elog, |
| 612 | bool ignore_existing_log) |
| 613 | { |
| 614 | int rc; |
| 615 | |
| 616 | rc = tcg2_platform_get_tpm2(dev); |
| 617 | if (rc) |
| 618 | return rc; |
| 619 | |
| 620 | rc = tpm_auto_start(*dev); |
| 621 | if (rc) |
| 622 | return rc; |
| 623 | |
| 624 | rc = tcg2_log_prepare_buffer(*dev, elog, ignore_existing_log); |
| 625 | if (rc) { |
| 626 | tcg2_measurement_term(*dev, elog, true); |
| 627 | return rc; |
| 628 | } |
| 629 | |
| 630 | rc = tcg2_measure_event(*dev, elog, 0, EV_S_CRTM_VERSION, |
| 631 | strlen(version_string) + 1, |
| 632 | (u8 *)version_string); |
| 633 | if (rc) { |
| 634 | tcg2_measurement_term(*dev, elog, true); |
| 635 | return rc; |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | void tcg2_measurement_term(struct udevice *dev, struct tcg2_event_log *elog, |
| 642 | bool error) |
| 643 | { |
| 644 | u32 event = error ? 0x1 : 0xffffffff; |
| 645 | int i; |
| 646 | |
| 647 | for (i = 0; i < 8; ++i) |
| 648 | tcg2_measure_event(dev, elog, i, EV_SEPARATOR, sizeof(event), |
| 649 | (const u8 *)&event); |
| 650 | |
| 651 | if (elog->log) |
| 652 | unmap_physmem(elog->log, MAP_NOCACHE); |
| 653 | } |
| 654 | |
| 655 | __weak int tcg2_platform_get_log(struct udevice *dev, void **addr, u32 *size) |
| 656 | { |
| 657 | const __be32 *addr_prop; |
| 658 | const __be32 *size_prop; |
| 659 | int asize; |
| 660 | int ssize; |
| 661 | |
| 662 | *addr = NULL; |
| 663 | *size = 0; |
| 664 | |
| 665 | addr_prop = dev_read_prop(dev, "tpm_event_log_addr", &asize); |
| 666 | if (!addr_prop) |
| 667 | addr_prop = dev_read_prop(dev, "linux,sml-base", &asize); |
| 668 | |
| 669 | size_prop = dev_read_prop(dev, "tpm_event_log_size", &ssize); |
| 670 | if (!size_prop) |
| 671 | size_prop = dev_read_prop(dev, "linux,sml-size", &ssize); |
| 672 | |
| 673 | if (addr_prop && size_prop) { |
| 674 | u64 a = of_read_number(addr_prop, asize / sizeof(__be32)); |
| 675 | u64 s = of_read_number(size_prop, ssize / sizeof(__be32)); |
| 676 | |
| 677 | *addr = map_physmem(a, s, MAP_NOCACHE); |
| 678 | *size = (u32)s; |
| 679 | } else { |
| 680 | struct ofnode_phandle_args args; |
| 681 | phys_addr_t a; |
| 682 | fdt_size_t s; |
| 683 | |
| 684 | if (dev_read_phandle_with_args(dev, "memory-region", NULL, 0, |
| 685 | 0, &args)) |
| 686 | return -ENODEV; |
| 687 | |
| 688 | a = ofnode_get_addr_size(args.node, "reg", &s); |
| 689 | if (a == FDT_ADDR_T_NONE) |
| 690 | return -ENOMEM; |
| 691 | |
| 692 | *addr = map_physmem(a, s, MAP_NOCACHE); |
| 693 | *size = (u32)s; |
| 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | __weak int tcg2_platform_get_tpm2(struct udevice **dev) |
| 700 | { |
| 701 | for_each_tpm_device(*dev) { |
| 702 | if (tpm_get_version(*dev) == TPM_V2) |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | return -ENODEV; |
| 707 | } |
| 708 | |
| 709 | u32 tcg2_algorithm_to_mask(enum tpm2_algorithms algo) |
| 710 | { |
| 711 | size_t i; |
| 712 | |
| 713 | for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) { |
| 714 | if (hash_algo_list[i].hash_alg == algo) |
| 715 | return hash_algo_list[i].hash_mask; |
| 716 | } |
| 717 | |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | __weak void tcg2_platform_startup_error(struct udevice *dev, int rc) {} |