Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 1 | /* |
Sandrine Bailleux | 4e9af17 | 2021-07-01 14:13:09 +0200 | [diff] [blame] | 2 | * Copyright (c) 2020-2021, Arm Limited. All rights reserved. |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <errno.h> |
| 9 | #include <string.h> |
| 10 | #include <arch_helpers.h> |
| 11 | |
| 12 | #include <common/bl_common.h> |
| 13 | #include <common/debug.h> |
| 14 | #include <drivers/auth/crypto_mod.h> |
Sandrine Bailleux | 3c2db6f | 2021-07-07 14:47:08 +0200 | [diff] [blame] | 15 | #include <drivers/measured_boot/event_log/event_log.h> |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 16 | #include <mbedtls/md.h> |
| 17 | |
| 18 | #include <plat/common/platform.h> |
| 19 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 20 | /* Running Event Log Pointer */ |
| 21 | static uint8_t *log_ptr; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 22 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 23 | /* Pointer to the first byte past end of the Event Log buffer */ |
| 24 | static uintptr_t log_end; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 25 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 26 | /* Pointer to event_log_metadata_t */ |
| 27 | static const event_log_metadata_t *plat_metadata_ptr; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 28 | |
| 29 | /* TCG_EfiSpecIdEvent */ |
| 30 | static const id_event_headers_t id_event_header = { |
| 31 | .header = { |
| 32 | .pcr_index = PCR_0, |
| 33 | .event_type = EV_NO_ACTION, |
| 34 | .digest = {0}, |
| 35 | .event_size = (uint32_t)(sizeof(id_event_struct_t) + |
| 36 | (sizeof(id_event_algorithm_size_t) * |
| 37 | HASH_ALG_COUNT)) |
| 38 | }, |
| 39 | |
| 40 | .struct_header = { |
| 41 | .signature = TCG_ID_EVENT_SIGNATURE_03, |
| 42 | .platform_class = PLATFORM_CLASS_CLIENT, |
| 43 | .spec_version_minor = TCG_SPEC_VERSION_MINOR_TPM2, |
| 44 | .spec_version_major = TCG_SPEC_VERSION_MAJOR_TPM2, |
| 45 | .spec_errata = TCG_SPEC_ERRATA_TPM2, |
| 46 | .uintn_size = (uint8_t)(sizeof(unsigned int) / |
| 47 | sizeof(uint32_t)), |
| 48 | .number_of_algorithms = HASH_ALG_COUNT |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | static const event2_header_t locality_event_header = { |
Sandrine Bailleux | be76143 | 2021-06-23 10:40:08 +0200 | [diff] [blame] | 53 | /* |
| 54 | * All EV_NO_ACTION events SHALL set |
| 55 | * TCG_PCR_EVENT2.pcrIndex = 0, unless otherwise specified |
| 56 | */ |
| 57 | .pcr_index = PCR_0, |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 58 | |
Sandrine Bailleux | be76143 | 2021-06-23 10:40:08 +0200 | [diff] [blame] | 59 | /* |
| 60 | * All EV_NO_ACTION events SHALL set |
| 61 | * TCG_PCR_EVENT2.eventType = 03h |
| 62 | */ |
| 63 | .event_type = EV_NO_ACTION, |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 64 | |
Sandrine Bailleux | be76143 | 2021-06-23 10:40:08 +0200 | [diff] [blame] | 65 | /* |
| 66 | * All EV_NO_ACTION events SHALL set TCG_PCR_EVENT2.digests to all |
| 67 | * 0x00's for each allocated Hash algorithm |
| 68 | */ |
| 69 | .digests = { |
| 70 | .count = HASH_ALG_COUNT |
| 71 | } |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 74 | /* |
Sandrine Bailleux | 36af1c8 | 2021-06-17 15:44:40 +0200 | [diff] [blame] | 75 | * Record a measurement as a TCG_PCR_EVENT2 event |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 76 | * |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 77 | * @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes |
| 78 | * @param[in] metadata_ptr Pointer to event_log_metadata_t structure |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 79 | * |
| 80 | * There must be room for storing this new event into the event log buffer. |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 81 | */ |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 82 | static void event_log_record(const uint8_t *hash, |
| 83 | const event_log_metadata_t *metadata_ptr) |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 84 | { |
| 85 | void *ptr = log_ptr; |
| 86 | uint32_t name_len; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 87 | |
Sandrine Bailleux | 898c099 | 2021-06-17 16:10:40 +0200 | [diff] [blame] | 88 | assert(hash != NULL); |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 89 | assert(metadata_ptr != NULL); |
| 90 | assert(metadata_ptr->name != NULL); |
| 91 | /* event_log_init() must have been called prior to this. */ |
| 92 | assert(log_ptr != NULL); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 93 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 94 | name_len = (uint32_t)strlen(metadata_ptr->name) + 1U; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 95 | |
| 96 | /* Check for space in Event Log buffer */ |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 97 | assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) < |
| 98 | log_end); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * As per TCG specifications, firmware components that are measured |
| 102 | * into PCR[0] must be logged in the event log using the event type |
| 103 | * EV_POST_CODE. |
| 104 | */ |
| 105 | /* TCG_PCR_EVENT2.PCRIndex */ |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 106 | ((event2_header_t *)ptr)->pcr_index = metadata_ptr->pcr; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 107 | |
| 108 | /* TCG_PCR_EVENT2.EventType */ |
| 109 | ((event2_header_t *)ptr)->event_type = EV_POST_CODE; |
| 110 | |
| 111 | /* TCG_PCR_EVENT2.Digests.Count */ |
| 112 | ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests); |
| 113 | ((tpml_digest_values *)ptr)->count = HASH_ALG_COUNT; |
| 114 | |
| 115 | /* TCG_PCR_EVENT2.Digests[] */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 116 | ptr = (uint8_t *)((uintptr_t)ptr + |
| 117 | offsetof(tpml_digest_values, digests)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 118 | |
| 119 | /* TCG_PCR_EVENT2.Digests[].AlgorithmId */ |
| 120 | ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID; |
| 121 | |
| 122 | /* TCG_PCR_EVENT2.Digests[].Digest[] */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 123 | ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 124 | |
Sandrine Bailleux | 898c099 | 2021-06-17 16:10:40 +0200 | [diff] [blame] | 125 | /* Copy digest */ |
| 126 | (void)memcpy(ptr, (const void *)hash, TCG_DIGEST_SIZE); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 127 | |
| 128 | /* TCG_PCR_EVENT2.EventSize */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 129 | ptr = (uint8_t *)((uintptr_t)ptr + TCG_DIGEST_SIZE); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 130 | ((event2_data_t *)ptr)->event_size = name_len; |
| 131 | |
| 132 | /* Copy event data to TCG_PCR_EVENT2.Event */ |
| 133 | (void)memcpy((void *)(((event2_data_t *)ptr)->event), |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 134 | (const void *)metadata_ptr->name, name_len); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 135 | |
| 136 | /* End of event data */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 137 | log_ptr = (uint8_t *)((uintptr_t)ptr + |
| 138 | offsetof(event2_data_t, event) + name_len); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 142 | * Initialise Event Log global variables, used during the recording |
| 143 | * of various payload measurements into the Event Log buffer |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 144 | * |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 145 | * @param[in] event_log_start Base address of Event Log buffer |
| 146 | * @param[in] event_log_finish End address of Event Log buffer, |
| 147 | * it is a first byte past end of the |
| 148 | * buffer |
| 149 | */ |
| 150 | void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish) |
| 151 | { |
| 152 | assert(event_log_start != NULL); |
| 153 | assert(event_log_finish > event_log_start); |
| 154 | |
| 155 | log_ptr = event_log_start; |
| 156 | log_end = (uintptr_t)event_log_finish; |
| 157 | |
| 158 | /* Get pointer to platform's event_log_metadata_t structure */ |
| 159 | plat_metadata_ptr = plat_event_log_get_metadata(); |
| 160 | assert(plat_metadata_ptr != NULL); |
| 161 | } |
| 162 | |
| 163 | /* |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 164 | * Initialises Event Log by writing Specification ID and |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 165 | * Startup Locality events |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 166 | */ |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 167 | void event_log_write_header(void) |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 168 | { |
| 169 | const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE; |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 170 | void *ptr = log_ptr; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 171 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 172 | /* event_log_init() must have been called prior to this. */ |
| 173 | assert(log_ptr != NULL); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * Add Specification ID Event first |
| 177 | * |
| 178 | * Copy TCG_EfiSpecIDEventStruct structure header |
| 179 | */ |
| 180 | (void)memcpy(ptr, (const void *)&id_event_header, |
| 181 | sizeof(id_event_header)); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 182 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_header)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 183 | |
| 184 | /* TCG_EfiSpecIdEventAlgorithmSize structure */ |
| 185 | ((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID; |
| 186 | ((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE; |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 187 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_algorithm_size_t)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * TCG_EfiSpecIDEventStruct.vendorInfoSize |
| 191 | * No vendor data |
| 192 | */ |
| 193 | ((id_event_struct_data_t *)ptr)->vendor_info_size = 0; |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 194 | ptr = (uint8_t *)((uintptr_t)ptr + |
| 195 | offsetof(id_event_struct_data_t, vendor_info)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * The Startup Locality event should be placed in the log before |
| 199 | * any event which extends PCR[0]. |
| 200 | * |
| 201 | * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3 |
| 202 | */ |
| 203 | |
| 204 | /* Copy Startup Locality Event Header */ |
| 205 | (void)memcpy(ptr, (const void *)&locality_event_header, |
| 206 | sizeof(locality_event_header)); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 207 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(locality_event_header)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 208 | |
| 209 | /* TCG_PCR_EVENT2.Digests[].AlgorithmId */ |
| 210 | ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID; |
| 211 | |
| 212 | /* TCG_PCR_EVENT2.Digests[].Digest[] */ |
| 213 | (void)memset(&((tpmt_ha *)ptr)->digest, 0, TPM_ALG_ID); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 214 | ptr = (uint8_t *)((uintptr_t)ptr + |
| 215 | offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 216 | |
| 217 | /* TCG_PCR_EVENT2.EventSize */ |
| 218 | ((event2_data_t *)ptr)->event_size = |
| 219 | (uint32_t)sizeof(startup_locality_event_t); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 220 | ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 221 | |
| 222 | /* TCG_EfiStartupLocalityEvent.Signature */ |
| 223 | (void)memcpy(ptr, (const void *)locality_signature, |
| 224 | sizeof(TCG_STARTUP_LOCALITY_SIGNATURE)); |
| 225 | |
| 226 | /* |
| 227 | * TCG_EfiStartupLocalityEvent.StartupLocality = 0: |
| 228 | * the platform's boot firmware |
| 229 | */ |
| 230 | ((startup_locality_event_t *)ptr)->startup_locality = 0U; |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 231 | log_ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Calculate and write hash of image, configuration data, etc. |
| 236 | * to Event Log. |
| 237 | * |
| 238 | * @param[in] data_base Address of data |
| 239 | * @param[in] data_size Size of data |
| 240 | * @param[in] data_id Data ID |
| 241 | * @return: |
| 242 | * 0 = success |
| 243 | * < 0 = error |
| 244 | */ |
Sandrine Bailleux | 4e9af17 | 2021-07-01 14:13:09 +0200 | [diff] [blame] | 245 | int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size, |
| 246 | uint32_t data_id) |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 247 | { |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 248 | unsigned char hash_data[MBEDTLS_MD_MAX_SIZE]; |
| 249 | int rc; |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 250 | const event_log_metadata_t *metadata_ptr = plat_metadata_ptr; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 251 | |
Sandrine Bailleux | 74b8e17 | 2021-06-23 15:44:18 +0200 | [diff] [blame] | 252 | /* Get the metadata associated with this image. */ |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 253 | while ((metadata_ptr->id != INVALID_ID) && |
| 254 | (metadata_ptr->id != data_id)) { |
| 255 | metadata_ptr++; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 256 | } |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 257 | assert(metadata_ptr->id != INVALID_ID); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 258 | |
| 259 | /* Calculate hash */ |
| 260 | rc = crypto_mod_calc_hash((unsigned int)MBEDTLS_MD_ID, |
| 261 | (void *)data_base, data_size, hash_data); |
| 262 | if (rc != 0) { |
| 263 | return rc; |
| 264 | } |
| 265 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 266 | event_log_record(hash_data, metadata_ptr); |
Sandrine Bailleux | 36af1c8 | 2021-06-17 15:44:40 +0200 | [diff] [blame] | 267 | |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 268 | return 0; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 272 | * Get current Event Log buffer size i.e. used space of Event Log buffer |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 273 | * |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 274 | * @param[in] event_log_start Base Pointer to Event Log buffer |
| 275 | * |
| 276 | * @return: current Size of Event Log buffer |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 277 | */ |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 278 | size_t event_log_get_cur_size(uint8_t *event_log_start) |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 279 | { |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 280 | assert(event_log_start != NULL); |
| 281 | assert(log_ptr >= event_log_start); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 282 | |
Manish V Badarkhe | 7ca9d65 | 2021-09-14 22:41:46 +0100 | [diff] [blame] | 283 | return (size_t)((uintptr_t)log_ptr - (uintptr_t)event_log_start); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 284 | } |