blob: ee54f9a510ca1b3493d14eac6b84637e4fd5da1f [file] [log] [blame]
Harrison Mutai762d4942025-05-06 13:06:41 +00001/*
2 * Copyright (c) 2020-2025, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef EVENT_LOG_H
8#define EVENT_LOG_H
9
10#include <stddef.h>
11#include <stdint.h>
12
13#include <tcg.h>
14
15/* Number of hashing algorithms supported */
16#define HASH_ALG_COUNT 1U
17
18#define EVLOG_INVALID_ID UINT32_MAX
19
20/* Maximum digest size based on the strongest hash algorithm i.e. SHA-512. */
21#define MAX_DIGEST_SIZE 64U
22
23#define MEMBER_SIZE(type, member) sizeof(((type *)0)->member)
24
25typedef struct {
26 unsigned int id;
27 const char *name;
28 unsigned int pcr;
29} event_log_metadata_t;
30
31typedef int (*evlog_hash_func_t)(uint32_t alg, void *data, unsigned int len,
32 uint8_t *digest);
33
34struct event_log_hash_info {
35 evlog_hash_func_t func;
36 const uint32_t *ids;
37 size_t count;
38};
39
Harrison Mutai88d6ec82025-08-12 14:32:58 +000040struct event_log_algorithm {
41 uint16_t id;
42 const char *name;
43 uint16_t size;
44};
45
Harrison Mutai762d4942025-05-06 13:06:41 +000046#define ID_EVENT_SIZE \
47 (sizeof(id_event_headers_t) + \
48 (sizeof(id_event_algorithm_size_t) * HASH_ALG_COUNT) + \
49 sizeof(id_event_struct_data_t))
50
51#define LOC_EVENT_SIZE \
52 (sizeof(event2_header_t) + sizeof(tpmt_ha) + TCG_DIGEST_SIZE + \
53 sizeof(event2_data_t) + sizeof(startup_locality_event_t))
54
55#define LOG_MIN_SIZE (ID_EVENT_SIZE + LOC_EVENT_SIZE)
56
57#define EVENT2_HDR_SIZE \
58 (sizeof(event2_header_t) + sizeof(tpmt_ha) + TCG_DIGEST_SIZE + \
59 sizeof(event2_data_t))
60
61/* Functions' declarations */
62
63/**
64 * Initialize the Event Log buffer.
65 *
66 * Sets global pointers to manage the Event Log memory region,
67 * allowing subsequent log operations to write into the buffer.
68 *
69 * @param[in] start Pointer to the start of the Event Log buffer.
70 * @param[in] finish Pointer to the end of the buffer
71 * (i.e., one byte past the last valid address).
72 *
73 * @return 0 on success, or -EINVAL if the input range is invalid.
74 */
75int event_log_buf_init(uint8_t *start, uint8_t *finish);
76
77/**
78 * Dump the contents of the Event Log.
79 *
80 * Outputs the raw contents of the Event Log buffer, typically
81 * for debugging or audit purposes.
82 *
83 * @param[in] log_addr Pointer to the start of the Event Log buffer.
84 * @param[in] log_size Size of the Event Log buffer in bytes.
85 *
86 * @return 0 on success, or a negative error code on failure.
87 */
88int event_log_dump(uint8_t *log_addr, size_t log_size);
89
90/**
91 * Initialize the Event Log subsystem.
92 *
93 * Wrapper around `event_log_buf_init()` to configure the memory range
94 * for the Event Log buffer.
95 *
96 * @param[in] start Pointer to the start of the Event Log buffer.
97 * @param[in] finish Pointer to the end of the buffer
98 * (i.e., one byte past the last valid address).
99 *
100 * @return 0 on success, or a negative error code on failure.
101 */
102int event_log_init(uint8_t *start, uint8_t *finish);
103
104/**
105 * Initialize the Event Log and register supported hash functions.
106 *
107 * Initializes the Event Log subsystem using the provided memory region
108 * and registers one or more cryptographic hash functions for use in
109 * event measurements. This function must be called before any measurements
110 * or event recording can take place.
111 *
112 * @param[in] start Pointer to the beginning of the Event Log buffer.
113 * @param[in] finish Pointer to the end of the Event Log buffer.
114 * @param[in] hash_info Pointer to a structure containing the hash function
115 * pointer and associated algorithm identifiers.
116 *
117 * @return 0 on success,
118 * -EEXIST if hash functions have already been registered,
119 * -EINVAL if the input parameters are invalid,
120 * or a negative error code from the underlying initialization logic.
121 */
122int event_log_init_and_reg(uint8_t *start, uint8_t *finish,
123 const struct event_log_hash_info *hash_info);
124
125/**
126 * Measure input data and log its hash to the Event Log.
127 *
128 * Computes the cryptographic hash of the specified data and records it
129 * in the Event Log as a TCG_PCR_EVENT2 structure using event type EV_POST_CODE.
130 * Useful for firmware or image attestation.
131 *
132 * @param[in] data_base Pointer to the base of the data to be measured.
133 * @param[in] data_size Size of the data in bytes.
134 * @param[in] data_id Identifier used to match against metadata.
135 * @param[in] metadata_ptr Pointer to an array of event_log_metadata_t.
136 *
137 * @return 0 on success, or a negative error code on failure.
138 */
139int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size,
140 uint32_t data_id,
141 const event_log_metadata_t *metadata_ptr);
142
143/**
144 * Measure the input data and return its hash.
145 *
146 * Computes the cryptographic hash of the specified memory region using
147 * the default hashing algorithm configured in the Event Log subsystem.
148 *
149 * @param[in] data_base Pointer to the base of the data to be measured.
150 * @param[in] data_size Size of the data in bytes.
151 * @param[out] hash_data Buffer to hold the resulting hash output
152 * (must be at least CRYPTO_MD_MAX_SIZE bytes).
153 *
154 * @return 0 on success, or an error code on failure.
155 */
156int event_log_measure(uintptr_t data_base, uint32_t data_size,
157 unsigned char *hash_data);
158
159/**
160 * Record a measurement event in the Event Log.
161 *
162 * Writes a TCG_PCR_EVENT2 structure to the Event Log using the
163 * provided hash and metadata. This function assumes the buffer
164 * has enough space and that `event_log_buf_init()` has been called.
165 *
166 * @param[in] hash Pointer to the digest (TCG_DIGEST_SIZE bytes).
167 * @param[in] event_type Type of the event, as defined in tcg.h.
168 * @param[in] metadata_ptr Pointer to an event_log_metadata_t structure
169 * providing event-specific context (e.g., PCR index, name).
170 *
171 * @return 0 on success, or -ENOMEM if the buffer has insufficient space.
172 */
173int event_log_record(const uint8_t *hash, uint32_t event_type,
174 const event_log_metadata_t *metadata_ptr);
175
176/**
177 * Initialize the Event Log with mandatory header events.
178 *
179 * Writes the Specification ID (SpecID) and Startup Locality events
180 * as required by the TCG PC Client Platform Firmware Profile.
181 * These must be the first entries in the Event Log.
182 *
183 * @return 0 on success, or a negative error code on failure.
184 */
185int event_log_write_header(void);
186
187/**
188 * Write the SpecID event to the Event Log.
189 *
190 * Records the TCG_EfiSpecIDEventStruct to declare the structure
191 * and supported algorithms of the Event Log format.
192 *
193 * @return 0 on success, or a negative error code on failure.
194 */
195int event_log_write_specid_event(void);
196
197/**
198 * Get the current size of the Event Log.
199 *
200 * Calculates how many bytes of the Event Log buffer have been used,
201 * based on the current log pointer and the start of the buffer.
202 *
203 * @param[in] start Pointer to the start of the Event Log buffer.
204 *
205 * @return The number of bytes currently used in the Event Log.
206 */
207size_t event_log_get_cur_size(uint8_t *start);
208
209#endif /* EVENT_LOG_H */