interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / aienos_ctl.c

258 lines · 10063 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* AIENOS M6-A control-transport stub. See aienos_ctl.h. No heap, clock or I/O. */
18#include "aienos_ctl.h"
19
20#include <string.h>
21
22#include "../argus/sha256.h"
23
24static const uint8_t CT_MAGIC[4] = {'A', 'C', 'T', '1'};
25/* Public derivation label: anyone can compute a TEST key. */
26static const char CT_TEST_LABEL[] = "AIENOS-M6A-TEST-IDENTITY/NOT-FOR-PRODUCTION/";
27
28static void wr16(uint8_t *b, uint16_t v) { b[0] = (uint8_t)(v >> 8); b[1] = (uint8_t)v; }
29static void wr32(uint8_t *b, uint32_t v)
30{
31 b[0] = (uint8_t)(v >> 24); b[1] = (uint8_t)(v >> 16); b[2] = (uint8_t)(v >> 8); b[3] = (uint8_t)v;
32}
33static uint16_t rd16(const uint8_t *b) { return (uint16_t)((uint16_t)b[0] << 8 | b[1]); }
34static uint32_t rd32(const uint8_t *b)
35{
36 return (uint32_t)b[0] << 24 | (uint32_t)b[1] << 16 | (uint32_t)b[2] << 8 | b[3];
37}
38
39static void hmac_sha256(const uint8_t key[32], const uint8_t *msg, size_t len, uint8_t out[32])
40{
41 uint8_t ipad[SHA256_BLOCK_SIZE], opad[SHA256_BLOCK_SIZE], inner[SHA256_DIGEST_SIZE];
42 memset(ipad, 0x36, sizeof ipad);
43 memset(opad, 0x5c, sizeof opad);
44 for (size_t i = 0; i < 32; i++) { ipad[i] ^= key[i]; opad[i] ^= key[i]; }
45 sha256_ctx c;
46 sha256_init(&c);
47 sha256_update(&c, ipad, sizeof ipad);
48 sha256_update(&c, msg, len);
49 sha256_final(&c, inner);
50 sha256_init(&c);
51 sha256_update(&c, opad, sizeof opad);
52 sha256_update(&c, inner, sizeof inner);
53 sha256_final(&c, out);
54}
55
56static int tag_equal(const uint8_t *a, const uint8_t *b)
57{
58 uint8_t d = 0;
59 for (size_t i = 0; i < CT_TAG_LEN; i++) d |= (uint8_t)(a[i] ^ b[i]);
60 return d == 0;
61}
62
63const char *ct_status_name(ct_status s)
64{
65 switch (s) {
66 case CT_OK: return "Ok";
67 case CT_DELIVERED: return "Delivered";
68 case CT_IDLE: return "Idle";
69 case CT_ERR_ARG: return "Arg";
70 case CT_ERR_MALFORMED: return "Malformed";
71 case CT_ERR_BAD_TAG: return "BadTag";
72 case CT_ERR_REFUSED: return "Refused";
73 case CT_ERR_BAD_ACK: return "BadAck";
74 case CT_ERR_CAPACITY: return "Capacity";
75 case CT_ERR_TOO_LARGE: return "TooLarge";
76 case CT_ERR_WINDOW_FULL: return "WindowFull";
77 case CT_ERR_LINK_DOWN: return "LinkDown";
78 case CT_ERR_SEQ_EXHAUSTED: return "SeqExhausted";
79 }
80 return "Unknown";
81}
82
83ct_status ct_test_identity(ct_identity *id, const char *label)
84{
85 if (!id || !label) return CT_ERR_ARG;
86 size_t n = strnlen(label, 65);
87 if (n == 0 || n > 64) return CT_ERR_ARG;
88 sha256_ctx c;
89 sha256_init(&c);
90 sha256_update(&c, (const uint8_t *)CT_TEST_LABEL, sizeof CT_TEST_LABEL - 1);
91 sha256_update(&c, (const uint8_t *)label, n);
92 sha256_final(&c, id->key);
93 id->kind = CT_IDENTITY_TEST;
94 return CT_OK;
95}
96
97ct_status ct_init(ct_endpoint *ep, const ct_identity *id, uint8_t role, uint32_t session,
98 uint32_t rto, uint32_t max_retries)
99{
100 if (!ep || !id || role > 1 || rto == 0) return CT_ERR_ARG;
101 if (id->kind != CT_IDENTITY_TEST) return CT_ERR_REFUSED; /* production identity: M5 */
102 memset(ep, 0, sizeof *ep);
103 memcpy(ep->key, id->key, 32);
104 ep->role = role;
105 ep->session = session;
106 ep->rto = rto;
107 ep->max_retries = max_retries;
108 return CT_OK;
109}
110
111uint32_t ct_in_flight(const ct_endpoint *ep) { return ep ? ep->next_seq - ep->base : 0; }
112
113ct_status ct_send(ct_endpoint *ep, const uint8_t *msg, size_t len)
114{
115 if (!ep || (!msg && len)) return CT_ERR_ARG;
116 if (ep->link_down) return CT_ERR_LINK_DOWN;
117 if (len > CT_MAX_PAYLOAD) return CT_ERR_TOO_LARGE;
118 if (ep->next_seq == UINT32_MAX) return CT_ERR_SEQ_EXHAUSTED;
119 if (ep->next_seq - ep->base >= CT_WINDOW) return CT_ERR_WINDOW_FULL;
120 ct_slot *s = &ep->slots[ep->next_seq % CT_WINDOW];
121 memset(s, 0, sizeof *s);
122 s->used = 1;
123 s->seq = ep->next_seq++;
124 s->len = (uint16_t)len;
125 if (len) memcpy(s->data, msg, len);
126 return CT_OK;
127}
128
129static size_t build_frame(const ct_endpoint *ep, uint8_t type, uint32_t seq, const uint8_t *p,
130 uint16_t len, uint8_t *out)
131{
132 memcpy(out, CT_MAGIC, 4);
133 out[4] = CT_VERSION;
134 out[5] = type;
135 out[6] = ep->role;
136 out[7] = CT_IDENTITY_TEST;
137 wr32(out + 8, ep->session);
138 wr32(out + 12, seq);
139 wr32(out + 16, ep->expected);
140 wr16(out + 20, len);
141 wr16(out + 22, 0);
142 if (len) memcpy(out + CT_HEADER_LEN, p, len);
143 uint8_t mac[32];
144 hmac_sha256(ep->key, out, CT_HEADER_LEN + (size_t)len, mac);
145 memcpy(out + CT_HEADER_LEN + len, mac, CT_TAG_LEN);
146 return CT_HEADER_LEN + (size_t)len + CT_TAG_LEN;
147}
148
149/* Resend wait for a slot that is already on the wire. The first retransmit
150 * waits exactly rto. Later ones wait rto plus a deterministic jitter in
151 * [0, rto], so the wait is bounded in [rto, 2*rto] and a strictly periodic
152 * loss pattern (drop every Nth datagram) cannot keep hitting the same frame.
153 * The jitter is a pure function of public connection state (session, role)
154 * and the slot (seq, tries): reproducible in tests, no clock, no randomness,
155 * no secret key bits, no new endpoint field, no wire change. */
156static uint64_t ct_mix(uint64_t x)
157{
158 x ^= x >> 30; x *= 0xbf58476d1ce4e5b9ull;
159 x ^= x >> 27; x *= 0x94d049bb133111ebull;
160 return x ^ (x >> 31);
161}
162
163static uint64_t ct_resend_wait(const ct_endpoint *ep, const ct_slot *s)
164{
165 uint64_t wait = ep->rto;
166 if (s->tries < 2) return wait; /* GUARD:ctl-jit-first */
167 uint64_t x = 0x9e3779b97f4a7c15ull;
168 x ^= (uint64_t)ep->session << 8; /* GUARD:ctl-jit-session */
169 x ^= (uint64_t)ep->role << 63; /* GUARD:ctl-jit-role */
170 x ^= (uint64_t)s->seq << 32; /* GUARD:ctl-jit-seq */
171 x ^= (uint64_t)s->tries * 0xd6e8feb86659fd93ull; /* GUARD:ctl-jit-tries */
172 x = ct_mix(x);
173 wait += x % ((uint64_t)ep->rto + 1u); /* GUARD:ctl-jit-add */
174 return wait;
175}
176
177ct_status ct_poll_tx(ct_endpoint *ep, uint64_t now, uint8_t *out, size_t cap, size_t *written)
178{
179 if (!ep || !out || !written) return CT_ERR_ARG;
180 *written = 0;
181 if (ep->link_down) return CT_ERR_LINK_DOWN;
182 for (uint32_t q = ep->base; q != ep->next_seq; q++) {
183 ct_slot *s = &ep->slots[q % CT_WINDOW];
184 if (!s->used || s->seq != q) continue;
185 int due = !s->sent || (now >= s->last_tx && now - s->last_tx >= ct_resend_wait(ep, s));
186 if (!due) continue;
187 if (s->sent && s->tries > ep->max_retries) { ep->link_down = 1; return CT_ERR_LINK_DOWN; }
188 if (cap < CT_HEADER_LEN + (size_t)s->len + CT_TAG_LEN) return CT_ERR_CAPACITY;
189 if (s->sent) ep->c.tx_retransmits++;
190 s->sent = 1;
191 s->tries++;
192 s->last_tx = now;
193 *written = build_frame(ep, CT_TYPE_DATA, s->seq, s->data, s->len, out);
194 ep->ack_pending = 0; /* piggybacked */
195 ep->c.tx_frames++;
196 return CT_OK;
197 }
198 if (ep->ack_pending) {
199 if (cap < CT_HEADER_LEN + CT_TAG_LEN) return CT_ERR_CAPACITY;
200 *written = build_frame(ep, CT_TYPE_ACK, 0, NULL, 0, out);
201 ep->ack_pending = 0;
202 ep->c.tx_acks++;
203 ep->c.tx_frames++;
204 return CT_OK;
205 }
206 return CT_IDLE;
207}
208
209ct_status ct_receive(ct_endpoint *ep, const uint8_t *f, size_t len, uint8_t *msg, size_t cap,
210 size_t *msg_len)
211{
212 if (!ep || (!f && len) || !msg_len || (!msg && cap)) return CT_ERR_ARG;
213 *msg_len = 0;
214 if (ep->link_down) return CT_ERR_LINK_DOWN;
215 /* 1. structure, before any field is trusted */
216 if (len < CT_HEADER_LEN + CT_TAG_LEN || memcmp(f, CT_MAGIC, 4) != 0 || f[4] != CT_VERSION ||
217 (f[5] != CT_TYPE_DATA && f[5] != CT_TYPE_ACK) || f[6] > 1 || rd16(f + 22) != 0) {
218 ep->c.rx_malformed++;
219 return CT_ERR_MALFORMED;
220 }
221 size_t plen = rd16(f + 20);
222 if (plen > CT_MAX_PAYLOAD || len != CT_HEADER_LEN + plen + CT_TAG_LEN) {
223 ep->c.rx_malformed++;
224 return CT_ERR_MALFORMED;
225 }
226 if (f[7] != CT_IDENTITY_TEST) { ep->c.rx_refused++; return CT_ERR_REFUSED; }
227 /* 2. authentication */
228 uint8_t mac[32];
229 hmac_sha256(ep->key, f, CT_HEADER_LEN + plen, mac);
230 if (!tag_equal(mac, f + CT_HEADER_LEN + plen)) { ep->c.rx_bad_tag++; return CT_ERR_BAD_TAG; }
231 /* 3. binding: our session, frames from the peer role only (no reflection) */
232 if (rd32(f + 8) != ep->session || f[6] == ep->role) { ep->c.rx_refused++; return CT_ERR_REFUSED; }
233 uint8_t type = f[5];
234 uint32_t seq = rd32(f + 12), ack = rd32(f + 16);
235 if (type == CT_TYPE_ACK && (seq != 0 || plen != 0)) { ep->c.rx_malformed++; return CT_ERR_MALFORMED; }
236 if (ack > ep->next_seq) { ep->c.rx_bad_ack++; return CT_ERR_BAD_ACK; }
237 if (type == CT_TYPE_DATA && seq == ep->expected && cap < plen) return CT_ERR_CAPACITY;
238 /* 4. cumulative ack (a stale ack below base is ignored: reordering) */
239 while (ep->base < ack) {
240 ct_slot *s = &ep->slots[ep->base % CT_WINDOW];
241 if (s->used && s->seq == ep->base) memset(s, 0, sizeof *s);
242 ep->base++;
243 }
244 if (type == CT_TYPE_ACK) { ep->c.rx_ack++; return CT_OK; }
245 /* 5. data: deliver exactly the next expected seq */
246 ep->ack_pending = 1;
247 if (seq == ep->expected) {
248 if (ep->expected == UINT32_MAX) { ep->c.rx_refused++; return CT_ERR_SEQ_EXHAUSTED; }
249 if (plen) memcpy(msg, f + CT_HEADER_LEN, plen);
250 *msg_len = plen;
251 ep->expected++;
252 ep->c.rx_delivered++;
253 return CT_DELIVERED;
254 }
255 if (seq < ep->expected) ep->c.rx_duplicate++;
256 else ep->c.rx_gap++;
257 return CT_OK;
258}