omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / fabric / fab_loopback.c
| 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 | /* fab_loopback.c -- loopback transport and HMAC authenticator. See fab_loopback.h. */ |
| 18 | #include "fab_loopback.h" |
| 19 | |
| 20 | #include "fab_hmac.h" |
| 21 | |
| 22 | #include <string.h> |
| 23 | |
| 24 | /* Link-map marker (test build only): tools/r16_prod_hygiene.sh refuses any |
| 25 | * production binary that carries an aien_test_build_* symbol. */ |
| 26 | __attribute__((used)) const char aien_test_build_fab_loopback[] = |
| 27 | "AIEN_TEST_BUILD piece: Fabric F5-0 loopback transport and HMAC stand-in authenticator"; |
| 28 | |
| 29 | static FabLoopBox *box_of(FabLoop *l, const AienMachineId *id) { |
| 30 | for (uint32_t i = 0; i < l->n; i++) |
| 31 | if (aien_mid_equal(&l->box[i].id, id)) return &l->box[i]; |
| 32 | return NULL; |
| 33 | } |
| 34 | |
| 35 | static int box_push(FabLoopBox *b, const uint8_t *msg, size_t len) { |
| 36 | if (b->count == FAB_LOOP_DEPTH || len > FAB_MSG_MAX) return FAB_E_TRANSPORT; |
| 37 | FabLoopMsg *m = &b->q[(b->head + b->count) % FAB_LOOP_DEPTH]; |
| 38 | m->len = (uint32_t)len; |
| 39 | memcpy(m->b, msg, len); |
| 40 | b->count++; |
| 41 | return FAB_OK; |
| 42 | } |
| 43 | |
| 44 | static int loop_send(void *ctx, const AienMachineId *from, const AienMachineId *to, |
| 45 | const uint8_t *msg, size_t len) { |
| 46 | FabLoop *l = ctx; |
| 47 | FabLoopBox *src = box_of(l, from), *dst = box_of(l, to); |
| 48 | if (!src || !dst || len > FAB_MSG_MAX) return FAB_E_TRANSPORT; |
| 49 | l->last.len = (uint32_t)len; |
| 50 | memcpy(l->last.b, msg, len); |
| 51 | if (src->silent) { l->dropped++; return FAB_OK; } |
| 52 | uint8_t hdr[2 * AIEN_MID_ID_BYTES + 4]; |
| 53 | memcpy(hdr, from->id, AIEN_MID_ID_BYTES); |
| 54 | memcpy(hdr + AIEN_MID_ID_BYTES, to->id, AIEN_MID_ID_BYTES); |
| 55 | for (int i = 0; i < 4; i++) hdr[2 * AIEN_MID_ID_BYTES + i] = (uint8_t)(len >> (8 * i)); |
| 56 | sha256_update(&l->transcript, hdr, sizeof hdr); |
| 57 | sha256_update(&l->transcript, msg, len); |
| 58 | l->sent++; |
| 59 | return box_push(dst, msg, len); |
| 60 | } |
| 61 | |
| 62 | static int loop_recv(void *ctx, const AienMachineId *self, uint8_t *buf, size_t cap, size_t *len) { |
| 63 | FabLoopBox *b = box_of(ctx, self); |
| 64 | if (!b) return FAB_E_TRANSPORT; |
| 65 | if (b->count == 0) return 0; |
| 66 | FabLoopMsg *m = &b->q[b->head]; |
| 67 | if (m->len > cap) return FAB_E_TRANSPORT; |
| 68 | memcpy(buf, m->b, m->len); |
| 69 | *len = m->len; |
| 70 | b->head = (b->head + 1) % FAB_LOOP_DEPTH; |
| 71 | b->count--; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | void fab_loop_init(FabLoop *l) { |
| 76 | memset(l, 0, sizeof *l); |
| 77 | sha256_init(&l->transcript); |
| 78 | l->transport.ctx = l; |
| 79 | l->transport.send = loop_send; |
| 80 | l->transport.recv = loop_recv; |
| 81 | } |
| 82 | |
| 83 | int fab_loop_add(FabLoop *l, const AienMachineId *id) { |
| 84 | if (!l || !id || box_of(l, id)) return FAB_E_ARG; |
| 85 | if (l->n == FAB_LOOP_NODES) return FAB_E_FULL; |
| 86 | l->box[l->n++].id = *id; |
| 87 | return FAB_OK; |
| 88 | } |
| 89 | |
| 90 | void fab_loop_silence(FabLoop *l, const AienMachineId *id, int silent) { |
| 91 | FabLoopBox *b = box_of(l, id); |
| 92 | if (b) b->silent = silent; |
| 93 | } |
| 94 | |
| 95 | int fab_loop_inject(FabLoop *l, const AienMachineId *to, const uint8_t *msg, size_t len) { |
| 96 | FabLoopBox *b = box_of(l, to); |
| 97 | return b ? box_push(b, msg, len) : FAB_E_TRANSPORT; |
| 98 | } |
| 99 | |
| 100 | uint32_t fab_loop_pending(const FabLoop *l) { |
| 101 | uint32_t p = 0; |
| 102 | for (uint32_t i = 0; i < l->n; i++) p += l->box[i].count; |
| 103 | return p; |
| 104 | } |
| 105 | |
| 106 | void fab_loop_transcript(const FabLoop *l, uint8_t out[32]) { |
| 107 | sha256_ctx c = l->transcript; |
| 108 | sha256_final(&c, out); |
| 109 | } |
| 110 | |
| 111 | static int hmac_sign(void *ctx, const uint8_t *msg, size_t len, uint8_t sig[FAB_SIG_BYTES]) { |
| 112 | const FabHmacAuth *a = ctx; |
| 113 | return fab_hmac_sig64(a->self_key, msg, len, sig); |
| 114 | } |
| 115 | |
| 116 | static int hmac_verify(void *ctx, const AienMachineId *claimed, const uint8_t *msg, size_t len, |
| 117 | const uint8_t sig[FAB_SIG_BYTES]) { |
| 118 | const FabHmacAuth *a = ctx; |
| 119 | for (uint32_t i = 0; i < a->n; i++) { |
| 120 | if (!aien_mid_equal(&a->ids[i], claimed)) continue; |
| 121 | uint8_t t[FAB_SIG_BYTES]; |
| 122 | if (fab_hmac_sig64(a->keys[i], msg, len, t)) return -1; |
| 123 | return fab_ct_equal(t, sig, FAB_SIG_BYTES) ? 0 : -1; |
| 124 | } |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | int fab_hmac_auth_init(FabHmacAuth *a, const AienMachineId *self, const uint8_t self_key[32], |
| 129 | uint32_t n, const AienMachineId *ids, const uint8_t (*keys)[32]) { |
| 130 | if (!a) return FAB_E_ARG; |
| 131 | memset(a, 0, sizeof *a); |
| 132 | if (!self || !self_key || !ids || !keys || n == 0 || n > FAB_HMAC_MAX_PEERS) return FAB_E_ARG; |
| 133 | a->self = *self; |
| 134 | memcpy(a->self_key, self_key, 32); |
| 135 | a->n = n; |
| 136 | a->ids = ids; |
| 137 | a->keys = keys; |
| 138 | a->auth.ctx = a; |
| 139 | a->auth.sign = hmac_sign; |
| 140 | a->auth.verify = hmac_verify; |
| 141 | return FAB_OK; |
| 142 | } |