ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
audit_log.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file audit_log.h
6 * @brief Tamper-evident, hash-chained audit log (PROTOCORE_ENABLE_AUDIT_LOG).
7 *
8 * An append-only security log where each entry carries
9 * `hash = SHA-256(prev_hash || seq || ts || category || msg)`, chaining every
10 * record to its predecessor. Altering, reordering, or deleting any retained
11 * record breaks the chain, which protocore_audit_verify() detects in O(n). All
12 * storage is a fixed RAM ring of PROTOCORE_AUDIT_LOG_ENTRIES records (no heap,
13 * bounded latency); when it wraps, the evicted record's hash becomes a moving
14 * chain anchor, so the *retained window* still verifies end-to-end.
15 *
16 * **Durability / forwarding.** The RAM ring is only the recent window for query
17 * and verification. Install a sink with protocore_audit_set_sink() to forward every
18 * record, at the moment it is created (before it can ever be evicted), to a
19 * durable or remote store - an SD-card file, a syslog / HTTP log service, a
20 * serial console. Because the sink receives the full record including its chain
21 * hash, the external store preserves the same tamper-evident chain. Use
22 * protocore_audit_format() to render a record as one JSON line for that sink.
23 *
24 * Pure and host-tested (the chain is the same on host and ESP32; SHA-256 comes
25 * from protocore_sha256, hardware-accelerated on ESP32). Single-accessor like the log
26 * buffer: append from one context (a worker / loop), not concurrently.
27 *
28 * @author Douglas Quigg (dstroy0)
29 * @date 2026
30 */
31
32#ifndef PROTOCORE_AUDIT_LOG_H
33#define PROTOCORE_AUDIT_LOG_H
34
35#include "protocore_config.h" // the entry point: protocore_types.h for the widths
36
37#if PROTOCORE_ENABLE_AUDIT_LOG
38
39EMBED_BEGIN_DECLS
40
41// PROTOCORE_AUDIT_LOG_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums it
42// into its arena. A caller takes them once and passes the pointer to every call. How they are
43// carved is this module's and is never named here.
44
45#define PROTOCORE_AUDIT_HASH_LEN 32
46
47/** @brief Standard audit event categories (extend with your own values). */
48EMBED_ENUM_BYTE(protocore_audit_cat,
49 PROTOCORE_AUDIT_SYSTEM = 0, ///< Boot, shutdown, time change, generic.
50 PROTOCORE_AUDIT_AUTH = 1, ///< Authentication success (login).
51 PROTOCORE_AUDIT_AUTH_FAIL = 2, ///< Authentication failure.
52 PROTOCORE_AUDIT_ACCESS = 3, ///< Resource access (request served / denied).
53 PROTOCORE_AUDIT_CONFIG = 4, ///< Configuration change.
54 PROTOCORE_AUDIT_ADMIN = 5, ///< Privileged / administrative action.
55);
56
57/** @brief One audit record. seq is monotonic and never reused across evictions. */
58typedef struct
59{
60 uint32_t seq; ///< Monotonic sequence number (1-based).
61 uint32_t ts; ///< Timestamp from protocore_millis() at append.
62 protocore_audit_cat category; ///< audit category (a ::protocore_audit_cat, or a user value cast in).
63 char msg[PROTOCORE_AUDIT_MSG_LEN]; ///< Null-terminated message (truncated).
64 uint8_t hash[PROTOCORE_AUDIT_HASH_LEN]; ///< SHA-256(prev_hash || fields).
65} protocore_audit_entry;
66
67/** @brief Sink invoked once per record, at append time, for durable forwarding. */
68typedef void (*protocore_audit_sink_fn)(const protocore_audit_entry *entry);
69
70/** @brief What set_sink takes. */
71typedef struct
72{
73 protocore_audit_sink_fn sink;
74} AuditLogSetSinkArgs;
75
76/** @brief What append takes. */
77typedef struct
78{
79 protocore_audit_cat category;
80 const char *msg;
81} AuditLogAppendArgs;
82
83/** @brief What at takes. */
84typedef struct
85{
86 uint16_t i;
87} AuditLogAtArgs;
88
89/** @brief What verify takes. */
90typedef struct
91{
92 uint32_t *first_broken_seq;
93} AuditLogVerifyArgs;
94
95/** @brief What cat_name takes. */
96typedef struct
97{
98 protocore_audit_cat category;
99} AuditLogCatNameArgs;
100
101/** @brief What format takes. */
102typedef struct
103{
104 const protocore_audit_entry *entry;
105 char *out;
106 size_t cap;
107} AuditLogFormatArgs;
108
109/** @brief What dump_json takes. */
110typedef struct
111{
112 char *out;
113 size_t cap;
114} AuditLogDumpJsonArgs;
115typedef struct
116{
117 AuditLogSetSinkArgs set_sink_args;
118 AuditLogAppendArgs append_args;
119 AuditLogAtArgs at_args;
120 AuditLogVerifyArgs verify_args;
121 AuditLogCatNameArgs cat_name_args;
122 AuditLogFormatArgs format_args;
123 AuditLogDumpJsonArgs dump_json_args;
124 embed_bool ok;
125 uint32_t ms;
126 uint16_t value;
127 const protocore_audit_entry *ptr;
128 const char *text;
129 int n;
130} AuditLogVars;
131
132/** @brief The operands and the outcome. */
133extern AuditLogVars AuditLogV;
134
135/** @brief The entries. */
136typedef struct
137{
138 void (*const reset)(uint8_t *work);
139 void (*const set_sink)(uint8_t *work);
140 void (*const append)(uint8_t *work);
141 void (*const count)(uint8_t *work);
142 void (*const at)(uint8_t *work);
143 void (*const verify)(uint8_t *work);
144 void (*const cat_name)(const uint8_t *work);
145 void (*const format)(const uint8_t *work);
146 void (*const dump_json)(uint8_t *work);
147} AuditLogNs;
148
149// What the table binds, defined once in the .c and taking one parameter each: everything
150// else an entry needs is an operand in AuditLogV or a region of the borrow at a fixed offset.
151void protocore_audit_log_reset(uint8_t *work);
152void protocore_audit_log_set_sink(uint8_t *work);
153void protocore_audit_log_append(uint8_t *work);
154void protocore_audit_log_count(uint8_t *work);
155void protocore_audit_log_at(uint8_t *work);
156void protocore_audit_log_verify(uint8_t *work);
157void protocore_audit_log_cat_name(const uint8_t *work);
158void protocore_audit_log_format(const uint8_t *work);
159void protocore_audit_log_dump_json(uint8_t *work);
160
161// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
162// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
163// `AuditLog.reset(work)` resolves to a named function and becomes a DIRECT call. An extern table
164// leaves the call indirect and the symbol live at every level, -O2 -flto included.
165static const AuditLogNs AuditLog __attribute__((unused)) = {
166 .reset = protocore_audit_log_reset,
167 .set_sink = protocore_audit_log_set_sink,
168 .append = protocore_audit_log_append,
169 .count = protocore_audit_log_count,
170 .at = protocore_audit_log_at,
171 .verify = protocore_audit_log_verify,
172 .cat_name = protocore_audit_log_cat_name,
173 .format = protocore_audit_log_format,
174 .dump_json = protocore_audit_log_dump_json,
175};
176
177/**
178 * @brief The PROTOCORE_AUDIT_LOG_BORROW bytes this module's state lives in.
179 *
180 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
181 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
182 * walks, so the state lasts the life of the program.
183 *
184 * @return the span.
185 */
186uint8_t *protocore_audit_log_span(void);
187
188EMBED_END_DECLS
189
190#endif // PROTOCORE_ENABLE_AUDIT_LOG
191
192#endif // PROTOCORE_AUDIT_LOG_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.