Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/runtime/rx_gen_work_auth.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / runtime / rx_gen_work_auth.c` 287 lines ยท 12085 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Proposer-owned generation work, without the aienos capability library. 5 * Cross-compile and run: 6 * aarch64-linux-gnu-gcc -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2 -Isrc -pthread \ 7 * -o rx_gen_work_auth tests/runtime/rx_gen_work_auth.c src/runtime/rx_generation.c \ 8 * src/omega_om_units.c -lm 9 * qemu-aarch64 -L /usr/aarch64-linux-gnu ./rx_gen_work_auth 10 */ 11 #include "runtime/rx_generation.h" 12 #include "sha256.h" 13 14 #include 15 #include 16 #include 17 #include 18 19 static int failures; 20 21 static void expect_int(const char *what, int got, int want) { 22 if (got != want) { 23 failures++; 24 printf("FAIL %s: got %d want %d\n", what, got, want); 25 } 26 } 27 28 static int caller_ok(void *ctx, uint32_t subject, const RxCallerCred *cred, int op) { 29 (void)ctx; 30 if (op == RX_CALLER_OP_RELEASE) return 0; 31 if (!cred || cred->generation != 1 || cred->secret[0] != (uint8_t)subject || subject == 0) 32 return RX_CALLER_ERR_FORGED; 33 return 0; 34 } 35 36 typedef struct { 37 uint32_t cap_id; 38 uint64_t generation; 39 uint32_t worker; 40 uint64_t resource; 41 uint32_t rights; 42 } ExpectAuth; 43 44 static int auth_exact(void *ctx, uint32_t cap_id, uint64_t generation, uint32_t subject, 45 uint64_t resource, uint32_t rights) { 46 ExpectAuth *e = ctx; 47 if (rights == RX_GEN_RIGHT_PROMOTE) 48 return resource == RX_GEN_RES_PROMOTION && subject == e->worker ? 0 : -1; 49 if (rights != RX_GEN_RIGHT_FINISH) return -1; 50 return cap_id == e->cap_id && generation == e->generation && subject == e->worker && 51 resource == e->resource && rights == e->rights 52 ? 0 53 : -1; 54 } 55 56 static RxCallerCred cred_for(uint32_t subject) { 57 RxCallerCred c; 58 memset(&c, 0, sizeof c); 59 c.generation = 1; 60 c.secret[0] = (uint8_t)subject; 61 return c; 62 } 63 64 static int open_store(char *dir, RxGenStore **out) { 65 if (!mkdtemp(dir)) return -1; 66 return rx_gen_open(dir, out); 67 } 68 69 static int propose(RxGenStore *g, uint32_t who, const RxCallerCred *cred, int bound, 70 const uint8_t *ev, size_t n, uint64_t *id) { 71 RxGenDraft d; 72 memset(&d, 0, sizeof d); 73 d.proofs_ok = 1; 74 d.evidence = ev; 75 d.evidence_len = n; 76 if (bound) return rx_gen_propose_as(g, who, cred, &d, id); 77 return rx_gen_propose(g, who, &d, id); 78 } 79 80 static void test_unbound(void) { 81 char dir[] = "/tmp/omega-wu-XXXXXX"; 82 RxGenStore *g = NULL; 83 expect_int("unbound open", open_store(dir, &g), RX_GEN_OK); 84 if (!g) return; 85 const uint8_t ev[] = "evidence-v1"; 86 uint64_t cand = 0; 87 expect_int("unbound propose", propose(g, 7, NULL, 0, ev, sizeof ev - 1, &cand), RX_GEN_OK); 88 RxGenWork item = {.id = 3, .class = RX_WORK_EVIDENCE, .state = RX_WORK_PENDING, .required = 1}; 89 expect_int("unbound add", rx_gen_add_work(g, cand, &item), RX_GEN_OK); 90 expect_int("unbound finish", rx_gen_finish_work(g, cand, item.id), RX_GEN_OK); 91 ExpectAuth promo = {.worker = 9}; 92 RxPromotionRequest req; 93 memset(&req, 0, sizeof req); 94 req.candidate_id = cand; 95 req.subject = 9; 96 req.cap_id = 1; 97 req.cap_generation = 1; 98 req.resource = RX_GEN_RES_PROMOTION; 99 req.rights = RX_GEN_RIGHT_PROMOTE; 100 expect_int("unbound promote", 101 rx_gen_promote(g, &req, auth_exact, &promo, NULL, NULL, NULL, NULL), RX_GEN_OK); 102 uint8_t blob[2048]; 103 size_t n = 0; 104 expect_int("unbound work blob", rx_gen_read_blob(g, cand, "work", blob, sizeof blob, &n), RX_GEN_OK); 105 uint8_t digest[32]; 106 sha256_hash(ev, sizeof ev - 1, digest); 107 expect_int("unbound provenance", 108 n == 16u + 80u && memcmp(blob, "R9WORK1", 7) == 0 && memcmp(blob + 16 + 48, digest, 32) == 0, 109 1); 110 rx_gen_close(g); 111 112 char dir2[] = "/tmp/omega-wu-XXXXXX"; 113 expect_int("unbound open 2", open_store(dir2, &g), RX_GEN_OK); 114 if (!g) return; 115 expect_int("unbound propose 2", propose(g, 7, NULL, 0, ev, sizeof ev - 1, &cand), RX_GEN_OK); 116 req.candidate_id = cand; 117 expect_int("unbound add 2", rx_gen_add_work(g, cand, &item), RX_GEN_OK); 118 expect_int("unbound finish 2", rx_gen_finish_work(g, cand, item.id), RX_GEN_OK); 119 const uint8_t swapped[] = "swapped-evidence"; 120 expect_int("swap after finish", rx_gen_set_evidence(g, cand, swapped, sizeof swapped - 1), RX_GEN_OK); 121 expect_int("swapped evidence refused", 122 rx_gen_promote(g, &req, auth_exact, &promo, NULL, NULL, NULL, NULL), RX_GEN_ERR_VERIFY); 123 rx_gen_close(g); 124 } 125 126 static void test_bound(void) { 127 enum { PROP = 4, OTHER = 5, WORKER = 6, PROM = 8 }; 128 char dir[] = "/tmp/omega-wb-XXXXXX"; 129 RxGenStore *g = NULL; 130 ExpectAuth auth = {.worker = PROM, .rights = RX_GEN_RIGHT_FINISH}; 131 expect_int("bound open", open_store(dir, &g), RX_GEN_OK); 132 if (!g) return; 133 expect_int("bind", rx_gen_bind_authority(g, caller_ok, NULL, auth_exact, &auth), RX_GEN_OK); 134 RxCallerCred prop = cred_for(PROP), other = cred_for(OTHER), worker = cred_for(WORKER), 135 prom = cred_for(PROM); 136 const uint8_t ev[] = "bound-evidence"; 137 uint64_t cand = 0; 138 expect_int("bound propose", propose(g, PROP, &prop, 1, ev, sizeof ev - 1, &cand), RX_GEN_OK); 139 RxGenWork item = {.id = 11, .class = RX_WORK_EVIDENCE, .state = RX_WORK_PENDING, .required = 1}; 140 item.finish_cap_id = 41; 141 item.finish_cap_generation = 7; 142 uint64_t resource = 0; 143 expect_int("resource", rx_gen_work_resource(g, cand, item.id, &resource), RX_GEN_OK); 144 auth.cap_id = item.finish_cap_id; 145 auth.generation = item.finish_cap_generation; 146 auth.worker = OTHER; 147 auth.resource = resource; 148 expect_int("other subject adds", rx_gen_add_work_as(g, OTHER, &other, cand, &item), 149 RX_GEN_ERR_AUTHORITY); 150 auth.worker = PROP; 151 auth.resource = resource ^ 1; 152 expect_int("wrong resource", rx_gen_add_work_as(g, PROP, &prop, cand, &item), RX_GEN_ERR_AUTHORITY); 153 auth.resource = resource; 154 expect_int("proposer adds", rx_gen_add_work_as(g, PROP, &prop, cand, &item), RX_GEN_OK); 155 expect_int("other finishes", rx_gen_finish_work_as(g, OTHER, &other, cand, item.id), 156 RX_GEN_ERR_AUTHORITY); 157 expect_int("bare finish", rx_gen_finish_work(g, cand, item.id), RX_GEN_ERR_IDENTITY); 158 expect_int("proposer finishes", rx_gen_finish_work_as(g, PROP, &prop, cand, item.id), RX_GEN_OK); 159 160 RxGenWork delegated = {.id = 12, .class = RX_WORK_EVIDENCE, .state = RX_WORK_PENDING, .required = 1}; 161 delegated.worker = WORKER; 162 delegated.worker_set = 1; 163 delegated.finish_cap_id = 42; 164 delegated.finish_cap_generation = 8; 165 uint64_t dres = 0; 166 expect_int("delegate resource", rx_gen_work_resource(g, cand, delegated.id, &dres), RX_GEN_OK); 167 expect_int("resources differ", dres != resource, 1); 168 auth.cap_id = item.finish_cap_id; 169 auth.generation = item.finish_cap_generation; 170 auth.worker = WORKER; 171 auth.resource = dres; 172 expect_int("cap reused across work", rx_gen_add_work_as(g, PROP, &prop, cand, &delegated), 173 RX_GEN_ERR_AUTHORITY); 174 auth.cap_id = delegated.finish_cap_id; 175 auth.generation = delegated.finish_cap_generation; 176 expect_int("delegate add", rx_gen_add_work_as(g, PROP, &prop, cand, &delegated), RX_GEN_OK); 177 auth.worker = PROP; 178 expect_int("proposer cannot finish delegate", 179 rx_gen_finish_work_as(g, PROP, &prop, cand, delegated.id), RX_GEN_ERR_AUTHORITY); 180 auth.worker = WORKER; 181 expect_int("delegate finishes", rx_gen_finish_work_as(g, WORKER, &worker, cand, delegated.id), 182 RX_GEN_OK); 183 RxGenWork done = {.id = 13, .class = RX_WORK_EVIDENCE, .state = RX_WORK_DONE, .required = 1}; 184 expect_int("already done refused", rx_gen_add_work_as(g, PROP, &prop, cand, &done), RX_GEN_ERR_ARG); 185 expect_int("duplicate id", rx_gen_add_work_as(g, PROP, &prop, cand, &item), RX_GEN_ERR_ARG); 186 187 auth.worker = PROM; 188 RxPromotionRequest req; 189 memset(&req, 0, sizeof req); 190 req.candidate_id = cand; 191 req.subject = PROM; 192 req.cap_id = 1; 193 req.cap_generation = 1; 194 req.resource = RX_GEN_RES_PROMOTION; 195 req.rights = RX_GEN_RIGHT_PROMOTE; 196 req.caller = prom; 197 expect_int("bound promote", rx_gen_promote(g, &req, auth_exact, &auth, NULL, NULL, NULL, NULL), 198 RX_GEN_OK); 199 rx_gen_close(g); 200 } 201 202 /* A shorter directory plus a parent id whose first byte continues that path 203 * must not share a finish resource with the longer directory. */ 204 static void test_path_prefix(void) { 205 char base[] = "/tmp/omega-wp-XXXXXX"; 206 if (!mkdtemp(base)) { 207 failures++; 208 printf("FAIL mkdtemp prefix\n"); 209 return; 210 } 211 char short_dir[256], long_dir[256]; 212 snprintf(short_dir, sizeof short_dir, "%s/a", base); 213 snprintf(long_dir, sizeof long_dir, "%s/ab", base); 214 RxGenStore *a = NULL, *b = NULL; 215 expect_int("open a", rx_gen_open(short_dir, &a), RX_GEN_OK); 216 expect_int("open ab", rx_gen_open(long_dir, &b), RX_GEN_OK); 217 if (!a || !b) return; 218 const uint8_t ev[] = "p"; 219 uint64_t ida = 0, idb = 0; 220 expect_int("propose a", propose(a, 1, NULL, 0, ev, 1, &ida), RX_GEN_OK); 221 expect_int("propose ab", propose(b, 1, NULL, 0, ev, 1, &idb), RX_GEN_OK); 222 uint64_t ra = 0, rb = 0; 223 expect_int("res a", rx_gen_work_resource(a, ida, 1, &ra), RX_GEN_OK); 224 expect_int("res ab", rx_gen_work_resource(b, idb, 1, &rb), RX_GEN_OK); 225 expect_int("path prefix resources differ", ra != rb, 1); 226 rx_gen_close(a); 227 rx_gen_close(b); 228 } 229 230 /* The finish resource must be the length-prefixed encoding 231 * sha256("omega.rx.generation.work.finish.v1" || le64(len(dir)) || dir || 232 * le64(parent) || le64(candidate) || le64(work))[0..8) little-endian, 0 -> UINT64_MAX. 233 * A real two-store collision of the raw encoding cannot be built: the fields after the 234 * directory are a fixed 24 bytes, so equal raw messages imply equal directories and ids. 235 * The length prefix is therefore pinned by this known-answer check, which fails if the 236 * prefix is dropped (the resource then equals the raw-encoding value checked below). */ 237 static void le64(uint8_t *p, uint64_t v) { 238 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); 239 } 240 241 static uint64_t resource_of(const char *dir, uint64_t parent, uint64_t cand, uint64_t work, 242 int prefixed) { 243 static const uint8_t domain[] = "omega.rx.generation.work.finish.v1"; 244 uint8_t len[8], ids[24], digest[32]; 245 le64(len, strlen(dir)); 246 le64(ids, parent); 247 le64(ids + 8, cand); 248 le64(ids + 16, work); 249 sha256_ctx ctx; 250 sha256_init(&ctx); 251 sha256_update(&ctx, domain, sizeof domain - 1); 252 if (prefixed) sha256_update(&ctx, len, sizeof len); 253 sha256_update(&ctx, (const uint8_t *)dir, strlen(dir)); 254 sha256_update(&ctx, ids, sizeof ids); 255 sha256_final(&ctx, digest); 256 uint64_t v = 0; 257 for (int i = 0; i < 8; i++) v |= (uint64_t)digest[i] << (8 * i); 258 return v ? v : UINT64_MAX; 259 } 260 261 static void test_resource_encoding(void) { 262 char dir[] = "/tmp/omega-wk-XXXXXX"; 263 RxGenStore *g = NULL; 264 expect_int("kat open", open_store(dir, &g), RX_GEN_OK); 265 if (!g) return; 266 uint64_t parent = 0, lineage = 0, cand = 0, res = 0; 267 expect_int("kat active", rx_gen_active(g, &parent, &lineage), RX_GEN_OK); 268 const uint8_t ev[] = "k"; 269 expect_int("kat propose", propose(g, 1, NULL, 0, ev, 1, &cand), RX_GEN_OK); 270 expect_int("kat resource", rx_gen_work_resource(g, cand, 3, &res), RX_GEN_OK); 271 expect_int("resource is the length-prefixed encoding", res == resource_of(dir, parent, cand, 3, 1), 1); 272 expect_int("resource is not the raw encoding", res != resource_of(dir, parent, cand, 3, 0), 1); 273 rx_gen_close(g); 274 } 275 276 int main(void) { 277 test_unbound(); 278 test_bound(); 279 test_path_prefix(); 280 test_resource_encoding(); 281 if (failures) { 282 printf("rx_gen_work_auth: FAIL %d\n", failures); 283 return 1; 284 } 285 printf("rx_gen_work_auth: PASS\n"); 286 return 0; 287 }