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 / kernel / artifact / osc_unit.c

542 lines · 22917 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/* osc_unit.c -- OSCUNIT admission. See osc_unit.h and OSC_UNIT_ARTIFACT.md
18 * (aien-protocols PR #17). The check order below is section 8.2, one comment
19 * per step, because the first failure decides the result. */
20#include <string.h>
21#include "osc_unit.h"
22#include "aienos_sig.h"
23#include "sha256.h"
24
25static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); }
26static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); }
27static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | ((uint64_t)rd32(p + 4) << 32); }
28
29static int all_zero(const uint8_t *p, size_t n)
30{
31 uint8_t acc = 0;
32 for (size_t i = 0; i < n; i++)
33 acc |= p[i];
34 return acc == 0;
35}
36
37static const char *const code_names[] = {
38 [0] = "OK", [1] = "TRUNCATED", [2] = "BAD_MAGIC", [3] = "CONTAINER_VERSION", [4] = "HEADER_FIELD",
39 [5] = "UNIT_FORMAT", [6] = "ABI_VERSION", [7] = "UNKNOWN_FLAGS", [8] = "TRAILING_BYTES",
40 [9] = "TOTAL_LENGTH", [10] = "SECTION_TABLE", [11] = "SECTION_BOUNDS", [12] = "SECTION_OVERLAP",
41 [13] = "SECTION_LAYOUT", [14] = "LIMIT_EXCEEDED", [15] = "IR_HASH_MISMATCH",
42 [16] = "CODE_HASH_MISMATCH", [17] = "ENTRY_HASH_MISMATCH", [18] = "CAPS_HASH_MISMATCH",
43 [19] = "IR_MALFORMED", [20] = "ENTRY_TABLE", [21] = "CAPS_TABLE", [22] = "SIGNER_CLASS",
44 [23] = "SIGNATURE_ALGORITHM", [24] = "TEST_SIGNER_IN_RELEASE", [25] = "UNTRUSTED_SIGNER",
45 [26] = "BAD_SIGNATURE", [27] = "CAP_DOMAIN_UNSUPPORTED", [28] = "CAP_GEN_NOT_REPRESENTABLE",
46 [29] = "CAP_GENERATION_STALE", [30] = "RESOURCE_UNAVAILABLE", [31] = "ENTRY_NAME",
47 [32] = "ENTRY_NAME_HASH", [33] = "ENTRY_NAME_DUPLICATE", [34] = "CODE_INSTRUCTION",
48};
49
50const char *osc_code_name(unsigned code)
51{
52 if (code < sizeof code_names / sizeof code_names[0] && code_names[code])
53 return code_names[code];
54 if (code == OSC_LAUNCH_BAD_ENTRY)
55 return "LAUNCH_BAD_ENTRY";
56 if (code == OSC_LAUNCH_ARG_SHAPE)
57 return "LAUNCH_ARG_SHAPE";
58 /* Realization receipt refusals (artifact/osc_receipt.h): 64 + the omega receipt spec's reason number. */
59 static const char *const rcpt_names[] = {
60 [0] = "RECEIPT_MISSING", [1] = "RECEIPT_SIZE", [2] = "RECEIPT_MAGIC", [3] = "RECEIPT_VERSION",
61 [4] = "RECEIPT_SIGNATURE_ALGORITHM", [5] = "RECEIPT_SIGNER_CLASS", [6] = "RECEIPT_EVIDENCE_CLASS",
62 [7] = "RECEIPT_EPOCH_ORDER", [9] = "RECEIPT_TEST_SIGNER_IN_PRODUCTION", [10] = "RECEIPT_UNTRUSTED_SIGNER",
63 [11] = "RECEIPT_BAD_SIGNATURE", [12] = "RECEIPT_REVOKED", [13] = "RECEIPT_STALE_EPOCH",
64 [14] = "RECEIPT_EXPIRED", [15] = "RECEIPT_NOT_YET_VALID", [16] = "RECEIPT_WRONG_TARGET",
65 [17] = "RECEIPT_WRONG_FIRMWARE", [18] = "RECEIPT_PROGRAM_MISMATCH", [19] = "RECEIPT_EXECUTABLE_MISMATCH",
66 [20] = "RECEIPT_ARGUMENT", [22] = "RECEIPT_UNTRUSTED_CHECKER", [23] = "RECEIPT_WRONG_PRODUCER",
67 [24] = "RECEIPT_DEPENDENCIES_MISMATCH", [32] = "RECEIPT_LRAT_PROOFS_REQUIRED",
68 };
69 if (code >= 64 && code - 64 < sizeof rcpt_names / sizeof rcpt_names[0] && rcpt_names[code - 64])
70 return rcpt_names[code - 64];
71 return "?";
72}
73
74/* ---------------------------------------------------------------------------
75 * Section 8.4: the OSC-emitted A64 subset. A port of the operation table
76 * (osc_a64.c:28-72), the variable-field masks (:76-93) and the decode
77 * round trip (:245-274) of omega at d64ccb3. A word passes when for some table
78 * entry (word & ~varmask(class)) == base AND the fields decoded from the word
79 * re-encode to the same word. The encoder refuses only the cases tested in
80 * round_trip_ok; every other field value re-encodes to itself.
81 * ------------------------------------------------------------------------- */
82enum { CL_RRR_SH, CL_RI, CL_R4, CL_MULH, CL_R3, CL_BFM, CL_CSINC, CL_MOV16, CL_MEM, CL_PAIR,
83 CL_B26, CL_BCOND, CL_CB, CL_BREG };
84enum { OP_PAIR_STP_OFF, OP_PAIR_STP_PRE, OP_PAIR_STP_POST, OP_PAIR_LDP_OFF, OP_PAIR_LDP_PRE,
85 OP_PAIR_LDP_POST, OP_OTHER };
86
87struct a64_op { uint32_t base; uint8_t cls; uint8_t pairop; };
88
89static const struct a64_op a64_ops[] = {
90 {0x8B000000u, CL_RRR_SH, OP_OTHER}, {0xCB000000u, CL_RRR_SH, OP_OTHER},
91 {0xAB000000u, CL_RRR_SH, OP_OTHER}, {0xEB000000u, CL_RRR_SH, OP_OTHER},
92 {0x91000000u, CL_RI, OP_OTHER}, {0xD1000000u, CL_RI, OP_OTHER},
93 {0xB1000000u, CL_RI, OP_OTHER}, {0xF1000000u, CL_RI, OP_OTHER},
94 {0x9B000000u, CL_R4, OP_OTHER}, {0x9B008000u, CL_R4, OP_OTHER},
95 {0x9B407C00u, CL_MULH, OP_OTHER}, {0x9BC07C00u, CL_MULH, OP_OTHER},
96 {0x9AC00C00u, CL_R3, OP_OTHER}, {0x9AC00800u, CL_R3, OP_OTHER},
97 {0x9AC02000u, CL_R3, OP_OTHER}, {0x9AC02400u, CL_R3, OP_OTHER}, {0x9AC02800u, CL_R3, OP_OTHER},
98 {0x8A000000u, CL_RRR_SH, OP_OTHER}, {0xAA000000u, CL_RRR_SH, OP_OTHER},
99 {0xCA000000u, CL_RRR_SH, OP_OTHER}, {0xAA200000u, CL_RRR_SH, OP_OTHER},
100 {0x93400000u, CL_BFM, OP_OTHER}, {0xD3400000u, CL_BFM, OP_OTHER},
101 {0x9A800400u, CL_CSINC, OP_OTHER},
102 {0xD2800000u, CL_MOV16, OP_OTHER}, {0x92800000u, CL_MOV16, OP_OTHER}, {0xF2800000u, CL_MOV16, OP_OTHER},
103 {0xF9400000u, CL_MEM, OP_OTHER}, {0xF9000000u, CL_MEM, OP_OTHER},
104 {0xA9000000u, CL_PAIR, OP_PAIR_STP_OFF}, {0xA9800000u, CL_PAIR, OP_PAIR_STP_PRE},
105 {0xA8800000u, CL_PAIR, OP_PAIR_STP_POST}, {0xA9400000u, CL_PAIR, OP_PAIR_LDP_OFF},
106 {0xA9C00000u, CL_PAIR, OP_PAIR_LDP_PRE}, {0xA8C00000u, CL_PAIR, OP_PAIR_LDP_POST},
107 {0x14000000u, CL_B26, OP_OTHER}, {0x94000000u, CL_B26, OP_OTHER},
108 {0x54000000u, CL_BCOND, OP_OTHER}, {0xB4000000u, CL_CB, OP_OTHER}, {0xB5000000u, CL_CB, OP_OTHER},
109 {0xD63F0000u, CL_BREG, OP_OTHER}, {0xD65F0000u, CL_BREG, OP_OTHER},
110 {0x38606800u, CL_R3, OP_OTHER}, /* ldrb (register) */
111};
112
113static uint32_t cls_varmask(unsigned cls)
114{
115 switch (cls) {
116 case CL_RRR_SH: return 0x00DFFFFFu;
117 case CL_RI: return 0x007FFFFFu;
118 case CL_R4: return 0x001F7FFFu;
119 case CL_MULH: return 0x001F03FFu;
120 case CL_R3: return 0x001F03FFu;
121 case CL_BFM: return 0x003FFFFFu;
122 case CL_CSINC: return 0x001FF3FFu;
123 case CL_MOV16: return 0x007FFFFFu;
124 case CL_MEM: return 0x003FFFFFu;
125 case CL_PAIR: return 0x003FFFFFu;
126 case CL_B26: return 0x03FFFFFFu;
127 case CL_BCOND: return 0x00FFFFEFu;
128 case CL_CB: return 0x00FFFFFFu;
129 case CL_BREG: return 0x000003E0u;
130 default: return 0;
131 }
132}
133
134/* The decoded fields re-encode to w unless the encoder refuses them. */
135static int round_trip_ok(const struct a64_op *op, uint32_t w)
136{
137 uint32_t rd = w & 31, rn = (w >> 5) & 31, ra = (w >> 10) & 31;
138 switch (op->cls) {
139 case CL_RRR_SH: return ((w >> 22) & 3) <= 2; /* LSL LSR ASR; ROR refused */
140 case CL_CSINC: return ((w >> 12) & 15) <= 14; /* cond NV refused */
141 case CL_BCOND: return (w & 15) <= 13; /* AL and NV refused */
142 case CL_BREG: return rn != 31; /* branch register is x0..x30 only */
143 case CL_PAIR: {
144 int wb = op->pairop == OP_PAIR_STP_PRE || op->pairop == OP_PAIR_STP_POST ||
145 op->pairop == OP_PAIR_LDP_PRE || op->pairop == OP_PAIR_LDP_POST;
146 int load = op->pairop == OP_PAIR_LDP_OFF || op->pairop == OP_PAIR_LDP_PRE ||
147 op->pairop == OP_PAIR_LDP_POST;
148 /* writeback base equal to a transfer register (rn is SP when 31, never equal) */
149 if (wb && rn != 31 && (rn == rd || rn == ra))
150 return 0;
151 if (load && rd == ra) /* Rt == Rt2, including XZR == XZR */
152 return 0;
153 return 1;
154 }
155 default: return 1;
156 }
157}
158
159int osc_a64_word_allowed(uint32_t w)
160{
161 /* brk: the compiler emits it only as the unreachable guard after a trap call, immediate = trap code. */
162 if ((w & 0xFFE0001Fu) == 0xD4200000u) {
163 uint32_t imm = (w >> 5) & 0xFFFFu;
164 return imm >= 1 && imm <= 14;
165 }
166 for (size_t i = 0; i < sizeof a64_ops / sizeof a64_ops[0]; i++) {
167 const struct a64_op *op = &a64_ops[i];
168 if ((w & ~cls_varmask(op->cls)) != op->base)
169 continue;
170 if (round_trip_ok(op, w))
171 return 1;
172 }
173 return 0;
174}
175
176/* ---------------------------------------------------------------------------
177 * Admission
178 * ------------------------------------------------------------------------- */
179struct sec { uint32_t off, len; const uint8_t *hash; };
180
181static int name_charset_ok(const uint8_t *n, unsigned len)
182{
183 for (unsigned i = 0; i < len; i++) {
184 uint8_t c = n[i];
185 int alpha = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_';
186 int digit = c >= '0' && c <= '9';
187 if (!(alpha || (digit && i > 0)))
188 return 0;
189 }
190 return len >= 1;
191}
192
193static void name_hash(const uint8_t *n, unsigned len, uint8_t out16[16])
194{
195 static const char tag[] = "AIENOS-OSC-FN-NAME-V1"; /* + the NUL below */
196 sha256_ctx c;
197 uint8_t d[32];
198 sha256_init(&c);
199 sha256_update(&c, (const uint8_t *)tag, sizeof tag); /* sizeof includes the NUL */
200 sha256_update(&c, n, len);
201 sha256_final(&c, d);
202 memcpy(out16, d, 16);
203}
204
205static int key_in(const uint8_t (*set)[32], unsigned n, const uint8_t fp[32], const uint8_t **key)
206{
207 for (unsigned i = 0; set && i < n; i++) {
208 uint8_t h[32];
209 sha256_hash(set[i], 32, h);
210 if (memcmp(h, fp, 32) == 0) {
211 *key = set[i];
212 return 1;
213 }
214 }
215 return 0;
216}
217
218/* Ed25519 group order L, little endian. */
219static const uint8_t ed_L[32] = {
220 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
222
223static int scalar_below_L(const uint8_t s[32])
224{
225 for (int i = 31; i >= 0; i--) {
226 if (s[i] != ed_L[i])
227 return s[i] < ed_L[i];
228 }
229 return 0; /* equal to L */
230}
231
232unsigned osc_unit_admit(const uint8_t *f, size_t len, const struct osc_policy *pol, struct osc_accept *out)
233{
234 struct sec sec[5]; /* 1-based */
235 uint32_t total;
236 unsigned ufv, cls, ncap = 0;
237 uint16_t nfn;
238 uint8_t digest[32];
239
240 if (!f || !pol)
241 return OSC_TRUNCATED;
242 if (pol->staging_max && len > pol->staging_max)
243 return OSC_RESOURCE_UNAVAILABLE;
244 /* 1 */
245 if (len < 128)
246 return OSC_TRUNCATED;
247 /* 2: magic, container version, fixed header fields and reserved bytes, unit format, ABI, flags */
248 if (memcmp(f, "OSCUNIT\0", 8) != 0)
249 return OSC_BAD_MAGIC;
250 if (rd16(f + 8) != 1)
251 return OSC_CONTAINER_VERSION;
252 if (rd16(f + 10) != 128 || rd32(f + 24) != 128 || rd16(f + 28) != 4 || rd16(f + 30) != 48 ||
253 !all_zero(f + 34, 2) || !all_zero(f + 50, 2) || !all_zero(f + 52, 4) || !all_zero(f + 60, 4) ||
254 !all_zero(f + 96, 32))
255 return OSC_HEADER_FIELD;
256 ufv = rd16(f + 12);
257 if (ufv >= 32 || !((pol->unit_formats >> ufv) & 1u))
258 return OSC_UNIT_FORMAT;
259 {
260 unsigned abi = rd16(f + 14);
261 if (abi >= 32 || !((pol->abi_versions >> abi) & 1u))
262 return OSC_ABI_VERSION;
263 }
264 if (rd32(f + 16) != 0)
265 return OSC_UNKNOWN_FLAGS;
266 /* 3 */
267 total = rd32(f + 20);
268 if (total < 384 || total > OSC_UNIT_MAX_BYTES)
269 return OSC_TOTAL_LENGTH;
270 if (len < total)
271 return OSC_TRUNCATED;
272 if (len > total)
273 return OSC_TRAILING_BYTES;
274 /* 4: the 320 header bytes are now in the file (total >= 384). */
275 for (unsigned k = 1; k <= 4; k++) {
276 const uint8_t *e = f + 128 + (k - 1) * 48;
277 if (rd16(e) != k || rd16(e + 2) != 0 || !all_zero(e + 12, 4))
278 return OSC_SECTION_TABLE;
279 sec[k].off = rd32(e + 4);
280 sec[k].len = rd32(e + 8);
281 sec[k].hash = e + 16;
282 }
283 {
284 uint64_t sigoff = (uint64_t)total - 64;
285 for (unsigned k = 1; k <= 4; k++)
286 if (sec[k].off < 320 || (uint64_t)sec[k].off + sec[k].len > sigoff)
287 return OSC_SECTION_BOUNDS;
288 for (unsigned a = 1; a <= 4; a++)
289 for (unsigned b = a + 1; b <= 4; b++)
290 if (sec[a].len && sec[b].len && sec[a].off < (uint64_t)sec[b].off + sec[b].len &&
291 sec[b].off < (uint64_t)sec[a].off + sec[a].len)
292 return OSC_SECTION_OVERLAP;
293 uint64_t pos = 320;
294 for (unsigned k = 1; k <= 4; k++) {
295 uint64_t want = (pos + 15) / 16 * 16;
296 if (sec[k].off != want)
297 return OSC_SECTION_LAYOUT;
298 if (want > pos && !all_zero(f + pos, (size_t)(want - pos)))
299 return OSC_SECTION_LAYOUT;
300 pos = want + sec[k].len;
301 }
302 if (pos != sigoff)
303 return OSC_SECTION_LAYOUT;
304 }
305 /* 5: limits (every read below lies in a bounds-checked section) */
306 nfn = rd16(f + 32);
307 {
308 uint32_t stack = rd32(f + 36);
309 uint64_t ticks = rd64(f + 40);
310 uint16_t pools = rd16(f + 48);
311 if (nfn < 1 || nfn > OSC_MAX_FUNCS)
312 return OSC_LIMIT_EXCEEDED;
313 if (stack < 4096 || stack > 1048576 || (stack % 16))
314 return OSC_LIMIT_EXCEEDED;
315 if (ticks < 1 || ticks > 1000000000ull)
316 return OSC_LIMIT_EXCEEDED;
317 if (pools > 64)
318 return OSC_LIMIT_EXCEEDED;
319 if (sec[1].len < 8 || sec[1].len > 1048576)
320 return OSC_LIMIT_EXCEEDED;
321 if (sec[2].len < 4 || sec[2].len > 262144 || (sec[2].len % 4))
322 return OSC_LIMIT_EXCEEDED;
323 if (sec[4].len >= 8) {
324 ncap = rd16(f + sec[4].off);
325 if (ncap > OSC_MAX_CAPS)
326 return OSC_LIMIT_EXCEEDED;
327 }
328 }
329 /* 6 */
330 cls = rd16(f + 56);
331 if (cls != OSC_SIGNER_TEST && cls != OSC_SIGNER_OWNER)
332 return OSC_SIGNER_CLASS;
333 if (rd16(f + 58) != 1)
334 return OSC_SIGNATURE_ALGORITHM;
335 /* 7: section hashes in order */
336 for (unsigned k = 1; k <= 4; k++) {
337 uint8_t h[32];
338 sha256_hash(f + sec[k].off, sec[k].len, h);
339 if (memcmp(h, sec[k].hash, 32) != 0)
340 return OSC_IR_HASH_MISMATCH + (k - 1);
341 }
342 /* 8 */
343 {
344 const uint8_t *ir = f + sec[1].off;
345 if (memcmp(ir, "OSC1IR\0", 7) != 0 || ir[7] != ufv)
346 return OSC_IR_MALFORMED;
347 }
348 /* 9: code scan */
349 {
350 const uint8_t *code = f + sec[2].off;
351 for (uint32_t o = 0; o < sec[2].len; o += 4)
352 if (!osc_a64_word_allowed(rd32(code + o)))
353 return OSC_CODE_INSTRUCTION;
354 }
355 /* 10: entry table. Locals hold the parsed records until acceptance. */
356 if (sec[3].len != 96u * nfn)
357 return OSC_ENTRY_TABLE;
358 {
359 uint64_t prev = 0;
360 int have_prev = 0;
361 for (unsigned i = 0; i < nfn; i++) {
362 const uint8_t *e = f + sec[3].off + 96u * i;
363 unsigned nregs = e[2], ret = e[3], nl;
364 if (rd16(e) != i)
365 return OSC_ENTRY_TABLE;
366 if (nregs > 6 || ret > 9 || !all_zero(e + 10, 2))
367 return OSC_ENTRY_TABLE;
368 for (unsigned r = 0; r < 6; r++) {
369 unsigned kind = e[4 + r];
370 if (r >= nregs) {
371 if (kind != 0)
372 return OSC_ENTRY_TABLE;
373 continue;
374 }
375 if (kind >= 1 && kind <= 9)
376 continue;
377 if (kind == 11 || kind == 12) {
378 if (ufv != 5 || r + 1 >= nregs || e[4 + r + 1] != 5)
379 return OSC_ENTRY_TABLE;
380 continue;
381 }
382 return OSC_ENTRY_TABLE;
383 }
384 uint32_t co = rd32(e + 12);
385 if ((co % 4) || co >= sec[2].len || (have_prev && co <= prev))
386 return OSC_ENTRY_TABLE;
387 prev = co;
388 have_prev = 1;
389 nl = e[32];
390 if (nl < 1 || nl > 63 || !all_zero(e + 33 + nl, 63 - nl) || !name_charset_ok(e + 33, nl))
391 return OSC_ENTRY_NAME;
392 uint8_t nh[16];
393 name_hash(e + 33, nl, nh);
394 if (memcmp(nh, e + 16, 16) != 0)
395 return OSC_ENTRY_NAME_HASH;
396 for (unsigned j = 0; j < i; j++) {
397 const uint8_t *q = f + sec[3].off + 96u * j;
398 if ((q[32] == nl && memcmp(q + 33, e + 33, nl) == 0) || memcmp(q + 16, e + 16, 16) == 0)
399 return OSC_ENTRY_NAME_DUPLICATE;
400 }
401 }
402 }
403 /* 11: capability table */
404 if (sec[4].len != 8 + 64u * ncap)
405 return OSC_CAPS_TABLE;
406 if (!all_zero(f + sec[4].off + 2, 6))
407 return OSC_CAPS_TABLE;
408 {
409 int have_prev = 0;
410 uint64_t prev = 0;
411 for (unsigned i = 0; i < ncap; i++) {
412 const uint8_t *e = f + sec[4].off + 8 + 64u * i;
413 unsigned kind = rd16(e), dom = rd16(e + 48);
414 uint32_t rid = rd32(e + 4), rights = rd32(e + 8), bk = rd32(e + 12), mops = rd32(e + 16);
415 uint64_t mbytes = rd64(e + 24), boff = rd64(e + 32), blen = rd64(e + 40);
416 if (!all_zero(e + 20, 4) || (kind != 2 && kind != 3) || rd16(e + 2) != 0 || rid == 0)
417 return OSC_CAPS_TABLE;
418 if (rights == 0 || (rights & ~63u) != 0 || mops == 0 || mbytes == 0)
419 return OSC_CAPS_TABLE;
420 if (kind == 3) {
421 if (bk != 1 || blen == 0 || mbytes > blen)
422 return OSC_CAPS_TABLE;
423 } else if (bk != 2 || boff != 0 || blen != 0) {
424 return OSC_CAPS_TABLE;
425 }
426 if ((dom != 1 && dom != 2) || rd16(e + 50) != 0 || !all_zero(e + 52, 4))
427 return OSC_CAPS_TABLE;
428 uint64_t key = ((uint64_t)dom << 40) | ((uint64_t)kind << 32) | rid;
429 if (have_prev && key <= prev)
430 return OSC_CAPS_TABLE;
431 prev = key;
432 have_prev = 1;
433 }
434 }
435 /* UnitDigest = SHA256("AIENOS-OSC-UNIT-V1\0" || file[0..320)) */
436 {
437 static const char tag[] = "AIENOS-OSC-UNIT-V1";
438 sha256_ctx c;
439 sha256_init(&c);
440 sha256_update(&c, (const uint8_t *)tag, sizeof tag);
441 sha256_update(&c, f, 320);
442 sha256_final(&c, digest);
443 }
444 /* 12 */
445 if (cls == OSC_SIGNER_TEST && pol->release)
446 return OSC_TEST_SIGNER_IN_RELEASE;
447 /* 13: the key comes from the loader's anchor set of that class, never from the container */
448 const uint8_t *key = 0;
449 if (cls == OSC_SIGNER_TEST ? !key_in(pol->test_anchors, pol->n_test, f + 64, &key)
450 : !key_in(pol->owner_anchors, pol->n_owner, f + 64, &key))
451 return OSC_UNTRUSTED_SIGNER;
452 /* 14: canonical S < L, then pure Ed25519 over the domain-separated digest */
453 /* scalar_below_L is defence in depth: the Ed25519 verifier also rejects a non-canonical S, so no test can separate them. */
454 {
455 static const char tag[] = "AIENOS-OSC-UNIT-SIGNATURE-V1";
456 uint8_t msg[sizeof tag + 32];
457 const uint8_t *sig = f + total - 64;
458 memcpy(msg, tag, sizeof tag);
459 memcpy(msg + sizeof tag, digest, 32);
460 if (!scalar_below_L(sig + 32) || aienos_ed25519_verify(sig, msg, sizeof msg, key) != 0)
461 return OSC_BAD_SIGNATURE;
462 }
463 /* 15: capability policy, three whole passes */
464 for (unsigned i = 0; i < ncap; i++) {
465 unsigned dom = rd16(f + sec[4].off + 8 + 64u * i + 48);
466 if (dom >= 32 || !((pol->cap_domains >> dom) & 1u))
467 return OSC_CAP_DOMAIN_UNSUPPORTED;
468 }
469 for (unsigned i = 0; i < ncap; i++) {
470 const uint8_t *e = f + sec[4].off + 8 + 64u * i;
471 if (rd16(e + 48) == 1 && (rd64(e + 56) >> 32) != 0)
472 return OSC_CAP_GEN_NOT_REPRESENTABLE;
473 }
474 for (unsigned i = 0; i < ncap; i++) {
475 const uint8_t *e = f + sec[4].off + 8 + 64u * i;
476 uint64_t want = rd64(e + 56), cur = 0;
477 if (want == 0 || !pol->gen_lookup)
478 continue;
479 if (pol->gen_lookup(pol->ctx, rd16(e + 48), rd16(e), rd32(e + 4), &cur) != 0 || cur != want)
480 return OSC_CAP_GENERATION_STALE;
481 }
482 /* Build the accepted view (everything below was validated above). */
483 struct osc_accept *a = out;
484 struct osc_accept local;
485 if (!a)
486 a = &local;
487 memset(a, 0, sizeof *a);
488 memcpy(a->unit_digest, digest, 32);
489 memcpy(a->ir_sha256, sec[1].hash, 32);
490 memcpy(a->code_sha256, sec[2].hash, 32);
491 memcpy(a->signer_fp, f + 64, 32);
492 a->unit_format = (uint16_t)ufv;
493 a->signer_class = (uint16_t)cls;
494 a->function_count = nfn;
495 a->pool_slots = rd16(f + 48);
496 a->cap_count = (uint16_t)ncap;
497 a->max_stack_bytes = rd32(f + 36);
498 a->cpu_ticks = rd64(f + 40);
499 a->ir_off = sec[1].off; a->ir_len = sec[1].len;
500 a->code_off = sec[2].off; a->code_len = sec[2].len;
501 for (unsigned i = 0; i < nfn; i++) {
502 const uint8_t *e = f + sec[3].off + 96u * i;
503 struct osc_entry *r = &a->entry[i];
504 r->fn_index = rd16(e);
505 r->nregs = e[2];
506 r->ret_kind = e[3];
507 memcpy(r->reg_kind, e + 4, 6);
508 r->code_offset = rd32(e + 12);
509 memcpy(r->name_hash, e + 16, 16);
510 r->name_len = e[32];
511 memcpy(r->name, e + 33, r->name_len);
512 r->name[r->name_len] = 0;
513 }
514 for (unsigned i = 0; i < ncap; i++) {
515 const uint8_t *e = f + sec[4].off + 8 + 64u * i;
516 struct osc_cap *c = &a->cap[i];
517 c->resource_kind = rd16(e);
518 c->resource_id = rd32(e + 4);
519 c->rights = rd32(e + 8);
520 c->bounds_kind = rd32(e + 12);
521 c->max_operations = rd32(e + 16);
522 c->max_bytes = rd64(e + 24);
523 c->bounds_offset = rd64(e + 32);
524 c->bounds_length = rd64(e + 40);
525 c->domain = rd16(e + 48);
526 c->required_generation = rd64(e + 56);
527 }
528 /* 16 */
529 if (pol->reserve && pol->reserve(pol->ctx, a) != 0)
530 return OSC_RESOURCE_UNAVAILABLE;
531 return OSC_OK;
532}
533
534int osc_unit_lookup(const struct osc_accept *a, const char *name, size_t name_len)
535{
536 if (!a || !name)
537 return -1;
538 for (unsigned i = 0; i < a->function_count && i < OSC_MAX_FUNCS; i++)
539 if (a->entry[i].name_len == name_len && memcmp(a->entry[i].name, name, name_len) == 0)
540 return a->entry[i].fn_index;
541 return -1;
542}