// // Copyright (c) 2026 BEL ESPRIT D ACCORD TRUST HOLDINGS INC // All rights reserved. #include #include #include #include #define MARLBORG_MAGIC 0x4D425748 /* "MBWH" */ typedef enum { MSG_HEARTBEAT = 0x01, MSG_RULE_INSTALL = 0x02, MSG_STATE_SYNC = 0x03 } marlborg_msg_type_t; typedef enum { ERR_NONE = 0, ERR_TRUNCATED = -1, ERR_BAD_MAGIC = -2, ERR_INVALID_TYPE = -3, ERR_LENGTH_MISMATCH = -4 } parse_error_t; #pragma pack(push, 1) typedef struct { uint32_t magic; uint8_t type; uint16_t length; uint8_t payload[]; } m_header_t; typedef struct { uint32_t rule_priority; uint16_t s_bh_fixed; uint16_t h_measured_fixed; uint8_t signature[64]; } rule_install_payload_t; typedef struct { uint16_t s_bh_fixed; uint16_t h_current_fixed; uint32_t chain_length; uint32_t step_count; uint8_t latest_hash[32]; } state_sync_payload_t; #pragma pack(pop) parse_error_t parse_marlborg_frame(const uint8_t* buffer, size_t len) { if (len < sizeof(m_header_t)) return ERR_TRUNCATED; const m_header_t* header = (const m_header_t*)buffer; if (ntohl(header->magic) != MARLBORG_MAGIC) return ERR_BAD_MAGIC; uint16_t expected_len = ntohs(header->length); if (len < (sizeof(m_header_t) + expected_len)) return ERR_LENGTH_MISMATCH; switch (header->type) { case MSG_HEARTBEAT: printf("[HEARTBEAT] alive\n"); return ERR_NONE; case MSG_RULE_INSTALL: { if (expected_len != sizeof(rule_install_payload_t)) return ERR_LENGTH_MISMATCH; const rule_install_payload_t* p = (const rule_install_payload_t*)header->payload; uint32_t prio = ntohl(p->rule_priority); uint16_t s_bh = ntohs(p->s_bh_fixed); uint16_t h_meas = ntohs(p->h_measured_fixed); printf("[AUTH GATE] Rule Install -> Prio: %u, S_BH: %u, H_meas: %u\n", prio, s_bh, h_meas); /* ICP-auth check: entropy must be within bounds */ if (h_meas > s_bh) { printf("[AUTH GATE] REJECTED: entropy overflow (%u > %u)\n", h_meas, s_bh); return ERR_NONE; } printf("[AUTH GATE] Entropy OK, forwarding to signature verification\n"); return ERR_NONE; } case MSG_STATE_SYNC: { if (expected_len != sizeof(state_sync_payload_t)) return ERR_LENGTH_MISMATCH; const state_sync_payload_t* p = (const state_sync_payload_t*)header->payload; printf("[STATE SYNC] S_BH=%u H=%u chain_len=%u step=%u\n", ntohs(p->s_bh_fixed), ntohs(p->h_current_fixed), ntohl(p->chain_length), ntohl(p->step_count)); return ERR_NONE; } default: return ERR_INVALID_TYPE; } }