File size: 3,068 Bytes
75619b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Copyright (c) 2026 BEL ESPRIT D ACCORD TRUST HOLDINGS INC
// All rights reserved.

#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <arpa/inet.h>

#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;
    }
}