Safe API Framework
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
sapi_checksum.c File Reference

CRC-64 implementation for data integrity checking. More...

Include dependency graph for sapi_checksum.c:

Go to the source code of this file.

Data Structures

struct  sapi_checksum_manager_t

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.

Variables

static const uint64_t g_crc64_ertms_table [256]
static const uint64_t g_crc64_iso_table [256]
static const uint64_t g_crc64_xz_table [256]
static sapi_checksum_manager_t g_checksum_manager = {0}
 Global checksum module state (single instance; no dynamic allocation).

Detailed Description

CRC-64 implementation for data integrity checking.

Implements ERTMS-compliant CRC-64-CCITT for redundant systems. Uses pre-computed lookup tables for O(1) byte-at-a-time computation.

MISRA C:2012 Compliance:

  • No dynamic memory allocation
  • Bounded execution time (lookup table based)
  • No recursion
  • Explicit type conversions
  • Comprehensive error handling

NOTE (fixed while wiring ADR-017's sapi_checkpoint, which depends on this module): this file previously did not compile at all - it referenced sapi_log_error/info/warn() and sapi_timer_get_ms(), neither of which exist anywhere in this codebase (the real logging API is sapi_log_write(level, tag, message), no varargs; the real timer query is sapi_timer_now(sapi_timestamp_ms_t *out_now_ms), an out-parameter, not a return value), and SAPI_STATUS_ERROR/SAPI_STATUS_INVALID, which are not members of sapi_status_t (see sapi_status.h - the real set includes SAPI_STATUS_INTERNAL_ERROR, SAPI_STATUS_DATA_CORRUPTION, SAPI_STATUS_NOT_INITIALIZED, SAPI_STATUS_INVALID_PARAM, etc.). It also was never wired into the top-level CMakeLists.txt feature list at all. Fixed here to the minimum needed to compile and behave self- consistently; the printf-style log calls were removed rather than rewritten against sapi_log_write's non-varargs signature, since logging is explicitly non-safety-path and out of scope for this pass.

NOTE (fixed while wiring safeAPIRBC2oo2's A/B cross-compare CRC64 integrity check): g_crc64_ertms_table, g_crc64_iso_table and g_crc64_xz_table previously had only ~24, 2, and 2 of their required 256 entries populated respectively (the rest implicitly zero- initialized by C) and were explicitly commented as placeholders - not a mathematically correct CRC-64 implementation, and explicitly called out as unsuitable for SIL-relevant integrity protection. All three are now full, correctly generated 256-entry tables, produced by the standard reflected/right-shifting CRC table-generation algorithm (matching this file's own byte-at-a-time update loop below: crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF]) against each polynomial constant already declared in sapi_checksum.h. There is no publicly standardized "CRC-64/ERTMS"; the ERTMS/ISO/XZ names and polynomial constants here are this codebase's own pre-existing (pre-dating this fix) convention, not a claim of conformance to an external CRC-64 standard of those names - so the generated tables are self-consistent with this module's own declared polynomials, which is what compute-then-verify integrity checking actually depends on.

Definition in file sapi_checksum.c.

Variable Documentation

◆ g_crc64_ertms_table

const uint64_t g_crc64_ertms_table[256]
static

CRC-64-CCITT lookup table (256 entries, 8 bytes each = 2KB) Polynomial: 0x1D4F63B86E40E541 (ERTMS standard, as named by this codebase - see file-level note above)

Full 256-entry table, generated by the standard reflected table algorithm (see file-level note above) against this polynomial.

Definition at line 69 of file sapi_checksum.c.

◆ g_crc64_iso_table

const uint64_t g_crc64_iso_table[256]
static

CRC-64-ISO lookup table (alternative polynomial) Polynomial: 0x000000000000001B (ISO 3309 / HDLC)

Full 256-entry table, generated by the standard reflected table algorithm (see file-level note above) against this polynomial. Values are small (< 0x20) because the polynomial itself only has 5 significant bits - this is mathematically correct for this (deliberately weak, non-standard) polynomial, not a bug.

Definition at line 168 of file sapi_checksum.c.

◆ g_crc64_xz_table

const uint64_t g_crc64_xz_table[256]
static

CRC-64-XZ lookup table (alternative polynomial) Polynomial: 0x142F0E1EBA9EA3C3 (XZ/LZMA, as named by this codebase - see file-level note above)

Full 256-entry table, generated by the standard reflected table algorithm (see file-level note above) against this polynomial.

Definition at line 265 of file sapi_checksum.c.

◆ g_checksum_manager

sapi_checksum_manager_t g_checksum_manager = {0}
static

Global checksum module state (single instance; no dynamic allocation).

Definition at line 369 of file sapi_checksum.c.