Safe Communication Framework 0.1.0 build feea4f9
ERTMS/ETCS safe communication (Subset-037 EuroRadio, Subset-098 RBC/RBC), CENELEC EN 50128 SIL 4 oriented
Loading...
Searching...
No Matches
saf.h
Go to the documentation of this file.
1
20#ifndef SAFECOMM_SAF_H
21#define SAFECOMM_SAF_H
22
23#include <stdbool.h>
24#include <stddef.h>
25#include <stdint.h>
26
27#include "safecomm/status.h"
28#include "safecomm/mac/mac.h"
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
36#define SAFECOMM_SAF_MAX_PDU 1024U
37
39#define SAFECOMM_SAF_AU1_LEN 13U
40#define SAFECOMM_SAF_AU2_LEN 21U
41#define SAFECOMM_SAF_AU3_LEN 9U
42#define SAFECOMM_SAF_AR_LEN 9U
44#define SAFECOMM_SAF_DATA_OVERHEAD 13U
45
47typedef enum
48{
49 SAFECOMM_SAF_FRAME_UNKNOWN = 0,
50 SAFECOMM_SAF_FRAME_AU1,
51 SAFECOMM_SAF_FRAME_AU2,
52 SAFECOMM_SAF_FRAME_AU3,
53 SAFECOMM_SAF_FRAME_DATA,
54 SAFECOMM_SAF_FRAME_DISCONNECT,
55 SAFECOMM_SAF_FRAME_AR
57
64
66typedef enum
67{
68 SAFECOMM_SAF_STATE_IDLE = 0,
69 SAFECOMM_SAF_STATE_WAIT_AU2,
70 SAFECOMM_SAF_STATE_WAIT_AU3,
71 SAFECOMM_SAF_STATE_DATA,
72 SAFECOMM_SAF_STATE_DISCONNECTING
74
76typedef struct
77{
80 uint32_t etcs_id_self;
81 uint32_t etcs_id_peer;
82 uint8_t ety;
83 uint32_t t_connect_ms;
86
91typedef struct
92{
94 safecomm_transport_t transport;
97 uint32_t seq_tx;
98 uint32_t seq_rx;
99 uint8_t ra[SAFECOMM_MAC_RAND_LEN];
100 uint8_t rb[SAFECOMM_MAC_RAND_LEN];
101 uint32_t now_ms;
102 uint32_t last_rx_ms;
103 bool supervision_armed;
106
116 const safecomm_saf_config_t *cfg,
117 const safecomm_transport_t *transport);
118
129
140 const uint8_t *pdu, size_t pdu_len);
141
156 uint8_t *out, size_t out_cap, size_t *out_len);
157
158/*
159 * Frame-driven responder API (REQ-SC-SAF-040..045).
160 *
161 * For a caller that owns its own I/O and cannot block, e.g. a cyclic executive where the peer's next frame arrives one
162 * or more cycles later over a relay shared by many connections. Each call consumes one already-received frame and
163 * produces at most one frame for the caller to transmit; none of them touches conn->transport. The same MAC and state
164 * checks as the blocking safecomm_saf_connect() / safecomm_saf_recv() apply (they share the implementation).
165 *
166 * The responder random RB is supplied by the caller: it must come from a cryptographically secure source, and in a
167 * 2oo2 system both channels must be given the same RB so they derive the same session key and emit identical frames.
168 */
169
175safecomm_saf_frame_kind_t safecomm_saf_frame_kind(const uint8_t *frame, size_t frame_len);
176
188safecomm_status_t safecomm_saf_au1_fields(const uint8_t *au1, size_t au1_len, uint32_t *out_peer_id,
189 uint8_t out_ra[SAFECOMM_MAC_RAND_LEN]);
190
204safecomm_status_t safecomm_saf_responder_on_au1(safecomm_saf_conn_t *conn, const uint8_t *au1, size_t au1_len,
205 const uint8_t rb[SAFECOMM_MAC_RAND_LEN],
206 uint8_t *out_au2, size_t out_cap, size_t *out_len);
207
220safecomm_status_t safecomm_saf_responder_on_au3(safecomm_saf_conn_t *conn, const uint8_t *au3, size_t au3_len,
221 uint8_t *out_ar, size_t out_cap, size_t *out_len);
222
228safecomm_status_t safecomm_saf_accept_frame(safecomm_saf_conn_t *conn, const uint8_t *frame, size_t frame_len,
229 uint8_t *out, size_t out_cap, size_t *out_len);
230
238
240#define SAFECOMM_SAF_DI_LEN 3U
241
255 size_t *out_len);
256
268void safecomm_saf_set_time(safecomm_saf_conn_t *conn, uint32_t now_ms);
269
278
279safecomm_status_t safecomm_saf_disconnect(safecomm_saf_conn_t *conn);
280
283
287typedef struct
288{
290 uint32_t seq_tx;
291 uint32_t seq_rx;
292 uint32_t seq_rx_last_accepted;
293 uint8_t ksmac[SAFECOMM_MAC_KEY_LEN];
294 uint8_t ra[SAFECOMM_MAC_RAND_LEN];
295 uint8_t rb[SAFECOMM_MAC_RAND_LEN];
296 uint32_t etcs_id_self;
297 uint32_t etcs_id_peer;
299
308
316 const safecomm_session_vital_state_t *in_state);
317
329 const uint8_t *pdu, size_t pdu_len,
330 uint8_t *out_frame, size_t out_frame_cap,
331 size_t *out_frame_len);
332
341 const uint8_t *frame, size_t frame_len);
342
343#ifdef __cplusplus
344}
345#endif
346
347#endif /* SAFECOMM_SAF_H */
Message Authentication Code and session-key derivation (ERTMS/ETCS Subset-037-2 §6....
#define SAFECOMM_MAC_RAND_LEN
Definition mac.h:38
#define SAFECOMM_MAC_KEY_LEN
Definition mac.h:34
safecomm_status_t safecomm_saf_commit_send(safecomm_saf_conn_t *conn, const uint8_t *frame, size_t frame_len)
Transmit an already prepared and cross-compared Sa-DATA frame.
Definition saf.c:623
void safecomm_saf_set_time(safecomm_saf_conn_t *conn, uint32_t now_ms)
Orderly Sa-DISCONNECT, then return the object to IDLE.
Definition saf.c:761
safecomm_saf_frame_kind_t
Frame kind by its header MTI, for a caller that routes received frames itself.
Definition saf.h:48
safecomm_status_t safecomm_saf_connect(safecomm_saf_conn_t *conn)
Run the AU1/AU2/AU3 handshake to completion (blocking).
Definition saf.c:241
safecomm_status_t safecomm_saf_responder_on_au3(safecomm_saf_conn_t *conn, const uint8_t *au3, size_t au3_len, uint8_t *out_ar, size_t out_cap, size_t *out_len)
Responder: verify AU3 and build the AR confirmation; the connection enters DATA.
Definition saf.c:519
safecomm_status_t safecomm_saf_responder_on_au1(safecomm_saf_conn_t *conn, const uint8_t *au1, size_t au1_len, const uint8_t rb[SAFECOMM_MAC_RAND_LEN], uint8_t *out_au2, size_t out_cap, size_t *out_len)
Responder: accept AU1, derive the session key from RA and the caller's rb, and build AU2.
Definition saf.c:490
safecomm_saf_state_t safecomm_saf_state(const safecomm_saf_conn_t *conn)
Current state (for logging / tests). Returns IDLE for NULL.
Definition saf.c:819
safecomm_saf_state_t
SaF state-machine state (observable, for logging / tests).
Definition saf.h:67
safecomm_status_t safecomm_saf_send(safecomm_saf_conn_t *conn, const uint8_t *pdu, size_t pdu_len)
Send one user PDU as a MAC-protected, sequence-numbered Sa-DATA.
Definition saf.c:643
safecomm_status_t safecomm_saf_recv(safecomm_saf_conn_t *conn, uint8_t *out, size_t out_cap, size_t *out_len)
Receive and verify one Sa-DATA PDU (blocking).
Definition saf.c:657
safecomm_status_t safecomm_saf_accept_frame(safecomm_saf_conn_t *conn, const uint8_t *frame, size_t frame_len, uint8_t *out, size_t out_cap, size_t *out_len)
Verify one received Sa-DATA or Sa-DISCONNECT frame (the checks of safecomm_saf_recv(),...
Definition saf.c:680
safecomm_status_t safecomm_saf_supervise(safecomm_saf_conn_t *conn)
Idle-link supervision (EN 50159 delay and loss): in DATA, if no valid frame arrived for cfg....
Definition saf.c:769
safecomm_status_t safecomm_saf_disconnect_frame(safecomm_saf_role_t role, uint8_t *out, size_t out_cap, size_t *out_len)
Build a Sa-DISCONNECT (reason 0, sub-reason 0) for a side of the given role, without any connection s...
Definition saf.c:563
safecomm_status_t safecomm_saf_prepare_send(safecomm_saf_conn_t *conn, const uint8_t *pdu, size_t pdu_len, uint8_t *out_frame, size_t out_frame_cap, size_t *out_frame_len)
Prepare and MAC-protect one Sa-DATA frame without transmitting it (for 2oo2 cross-checking).
Definition saf.c:581
safecomm_status_t safecomm_saf_au1_fields(const uint8_t *au1, size_t au1_len, uint32_t *out_peer_id, uint8_t out_ra[SAFECOMM_MAC_RAND_LEN])
Read the caller's ETCS identity and initiator random RA out of an AU1 frame (no MAC exists on AU1)....
Definition saf.c:474
safecomm_saf_frame_kind_t safecomm_saf_frame_kind(const uint8_t *frame, size_t frame_len)
Classify a received frame by its header MTI (no MAC check).
Definition saf.c:454
safecomm_status_t safecomm_saf_frame_sent(safecomm_saf_conn_t *conn)
Record that the caller transmitted the frame from safecomm_saf_prepare_send() itself: advances the se...
Definition saf.c:549
safecomm_saf_role_t
SaF connection role.
Definition saf.h:60
@ SAFECOMM_SAF_ROLE_RESPONDER
Definition saf.h:62
@ SAFECOMM_SAF_ROLE_INITIATOR
Definition saf.h:61
safecomm_status_t safecomm_saf_import_session_state(safecomm_saf_conn_t *conn, const safecomm_session_vital_state_t *in_state)
Import vital session state upon warm-standby takeover or channel resynchronization.
Definition saf.c:846
safecomm_status_t safecomm_saf_export_session_state(const safecomm_saf_conn_t *conn, safecomm_session_vital_state_t *out_state)
Export vital session state for warm/hot standby synchronization.
Definition saf.c:824
safecomm_status_t safecomm_saf_init(safecomm_saf_conn_t *conn, const safecomm_saf_config_t *cfg, const safecomm_transport_t *transport)
Initialise a SaF connection object (no I/O yet).
Definition saf.c:125
Common status codes for the Safe Communication Framework.
safecomm_status_t
Result of a Safe Communication Framework operation.
Definition status.h:17
Immutable session-key material for one SaF connection.
Definition mac.h:48
Static configuration for one SaF connection.
Definition saf.h:77
uint32_t etcs_id_peer
Definition saf.h:81
uint8_t kmac[SAFECOMM_MAC_KEY_LEN]
Definition saf.h:79
uint32_t t_connect_ms
Definition saf.h:83
uint32_t etcs_id_self
Definition saf.h:80
uint32_t t_supervision_ms
Definition saf.h:84
SaF connection object. Caller-allocated, zero-initialised, opaque in practice — fields are not part o...
Definition saf.h:92
uint32_t last_rx_ms
Definition saf.h:102
uint32_t connect_start_ms
Definition saf.h:104
uint32_t now_ms
Definition saf.h:101
Vital session state for 2oo2 hot redundancy and active/warm-standby failover.
Definition saf.h:288
A consumer-supplied bearer the SaF layer runs over.
Definition transport.h:56
Transport abstraction for the Safe Communication Framework.