|
Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
|
Data integrity verification for redundant communication. More...
Files | |
| file | src/redundancy/checksum/sapi_checksum.c |
| CRC-64 implementation for data integrity checking. | |
Data Structures | |
| struct | sapi_checksum_result_t |
| Checksum result for validation. More... | |
| Vital channel message with integrated CRC-64. More... | |
| struct | sapi_checksum_stats_t |
| Checksum statistics for monitoring. More... | |
Typedefs | |
| typedef uint64_t | sapi_crc64_t |
| CRC-64 checksum value (64-bit). | |
Enumerations | |
| enum | sapi_crc64_polynomial_t { SAPI_CRC64_ERTMS , SAPI_CRC64_ISO , SAPI_CRC64_XZ } |
| CRC-64 polynomial selection. More... | |
Functions | |
| sapi_status_t | sapi_checksum_crc64_init (sapi_crc64_polynomial_t polynomial) |
| Initialize CRC-64 lookup tables for chosen polynomial. | |
| sapi_crc64_t | sapi_checksum_crc64 (const uint8_t *data, size_t size) |
| Compute CRC-64 for data buffer. | |
| sapi_status_t | sapi_checksum_crc64_verify (const uint8_t *data, size_t size, sapi_crc64_t expected_crc, sapi_checksum_result_t *result_out) |
| Verify CRC-64 of data against stored value. | |
| sapi_crc64_polynomial_t | sapi_checksum_crc64_get_polynomial (void) |
| Get current CRC-64 polynomial in use. | |
| sapi_status_t | sapi_checksum_vital_message_create (sapi_vital_message_t *msg_out, uint32_t sender_id, uint32_t sequence, const uint8_t *payload, size_t payload_size) |
| Create vital channel message with CRC. | |
| sapi_status_t | sapi_checksum_vital_message_verify (const sapi_vital_message_t *msg, uint32_t expected_sequence, uint8_t *payload_out, size_t payload_max_size, uint8_t *payload_size_out) |
| Verify vital channel message and extract payload. | |
| sapi_status_t | sapi_checksum_get_stats (sapi_checksum_stats_t *stats_out) |
| Get checksum statistics. | |
| sapi_status_t | sapi_checksum_reset_stats (void) |
| Reset checksum statistics. | |
Data integrity verification for redundant communication.
REQ-CHECKSUM-001: sapi_checksum_crc64_init() shall be callable exactly once; a subsequent call before any re-init mechanism exists shall return SAPI_STATUS_ALREADY_INITIALIZED and leave the already-selected table/polynomial unchanged. REQ-CHECKSUM-002: sapi_checksum_crc64() shall return 0 - never dereferencing data - if the module is not yet initialized, if its lookup table is unset, or if data is NULL while size is nonzero. REQ-CHECKSUM-003: sapi_checksum_crc64() shall be deterministic and O(n) in size, using a precomputed 256-entry lookup table (no bit-by-bit computation on the hot path). REQ-CHECKSUM-004: sapi_checksum_crc64_verify() shall report SAPI_STATUS_DATA_CORRUPTION (not merely a boolean) on mismatch and increment stats.verification_failures; on match it shall return SAPI_STATUS_OK and increment stats.verification_passes. REQ-CHECKSUM-005: sapi_checksum_vital_message_create() shall reject a payload larger than sizeof(sapi_vital_message_t::payload) with SAPI_STATUS_INVALID_PARAM, incrementing stats.payload_oversize, without writing msg_out. REQ-CHECKSUM-006: sapi_checksum_vital_message_verify() shall verify the message's CRC-64 before trusting any other field, and report SAPI_STATUS_DATA_CORRUPTION - without writing to payload_out/payload_size_out - on either a CRC mismatch or a sequence_number that does not equal the caller-supplied expected_sequence (incrementing stats.sequence_errors in the latter case). REQ-CHECKSUM-007: sapi_checksum_vital_message_verify() shall reject a decoded payload_size exceeding the caller's payload_max_size with SAPI_STATUS_INVALID_PARAM, incrementing stats.payload_oversize, without copying into payload_out. REQ-CHECKSUM-008: sapi_checksum_get_stats()/_reset_stats() are diagnostics-only (never on a safety-decision path); _get_stats() returns SAPI_STATUS_INVALID_PARAM for a NULL stats_out, otherwise both always return SAPI_STATUS_OK.
| typedef uint64_t sapi_crc64_t |
CRC-64 checksum value (64-bit).
Definition at line 100 of file sapi_checksum.h.
CRC-64 polynomial selection.
Different polynomials for different use cases and standards.
Definition at line 77 of file sapi_checksum.h.
| sapi_status_t sapi_checksum_crc64_init | ( | sapi_crc64_polynomial_t | polynomial | ) |
Initialize CRC-64 lookup tables for chosen polynomial.
Must be called once at startup before using CRC-64 functions. Generates lookup tables for O(1) byte-at-a-time computation.
| polynomial | CRC polynomial to use (ERTMS, ISO, or XZ) |
Example:
Definition at line 375 of file sapi_checksum.c.
| sapi_crc64_t sapi_checksum_crc64 | ( | const uint8_t * | data, |
| size_t | size ) |
Compute CRC-64 for data buffer.
Computes CRC-64 using pre-computed lookup tables (O(1) per byte). Execution time is deterministic and bounded.
| data | Data buffer to checksum (may be NULL if size=0) |
| size | Size in bytes (0 to MAX_SIZE) |
Note: Call sapi_checksum_crc64_init() before first use.
Example:
Definition at line 407 of file sapi_checksum.c.
| sapi_status_t sapi_checksum_crc64_verify | ( | const uint8_t * | data, |
| size_t | size, | ||
| sapi_crc64_t | expected_crc, | ||
| sapi_checksum_result_t * | result_out ) |
Verify CRC-64 of data against stored value.
Computes CRC-64 and compares to expected value. Returns detailed result for diagnostics.
| data | Data buffer to verify |
| size | Size in bytes |
| expected_crc | Expected CRC-64 value (from message) |
| result_out | Receives verification result |
Example:
Definition at line 460 of file sapi_checksum.c.
| sapi_crc64_polynomial_t sapi_checksum_crc64_get_polynomial | ( | void | ) |
Get current CRC-64 polynomial in use.
Returns the polynomial that was initialized at startup. Useful for logging and diagnostics.
Definition at line 493 of file sapi_checksum.c.
| sapi_status_t sapi_checksum_vital_message_create | ( | sapi_vital_message_t * | msg_out, |
| uint32_t | sender_id, | ||
| uint32_t | sequence, | ||
| const uint8_t * | payload, | ||
| size_t | payload_size ) |
Create vital channel message with CRC.
Wraps payload data with sequence number, timestamp, and CRC-64.
| msg_out | Message to fill |
| sender_id | ID of sending channel/site |
| sequence | Sequence number for this message |
| payload | Data to send |
| payload_size | Size of payload (max 248 bytes) |
Example:
Definition at line 502 of file sapi_checksum.c.
| sapi_status_t sapi_checksum_vital_message_verify | ( | const sapi_vital_message_t * | msg, |
| uint32_t | expected_sequence, | ||
| uint8_t * | payload_out, | ||
| size_t | payload_max_size, | ||
| uint8_t * | payload_size_out ) |
Verify vital channel message and extract payload.
Verifies CRC-64, checks sequence number continuity, and extracts payload. Returns error if any check fails.
| msg | Received message to verify |
| expected_sequence | Expected sequence number (for continuity check) |
| payload_out | Buffer to receive extracted payload |
| payload_max_size | Max size of payload buffer |
| payload_size_out | Receives actual payload size |
Example:
Definition at line 556 of file sapi_checksum.c.
| sapi_status_t sapi_checksum_get_stats | ( | sapi_checksum_stats_t * | stats_out | ) |
Get checksum statistics.
Returns counters useful for monitoring data integrity and diagnosing communication issues.
| stats_out | Receives statistics |
Definition at line 616 of file sapi_checksum.c.
| sapi_status_t sapi_checksum_reset_stats | ( | void | ) |
Reset checksum statistics.
Clears all counters. Useful for per-cycle diagnostics.
Definition at line 626 of file sapi_checksum.c.