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