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