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/aienux/blob/main/native/kernel/artifact/format.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / artifact / format.c` 395 lines ยท 15202 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 /* format.c -- SEED-0B artifact parse, capability and envelope records, 18 * identity digests and signature verification (format.rs, capability.rs, 19 * resource.rs, id.rs, canonical.rs, verify.rs). */ 20 #include "ck_artifact.h" 21 #include "aienos_sig.h" 22 #include "sha256.h" 23 24 #define DOM(s) (s), (sizeof(s)) 25 26 /* RFC 8032 section 7.1 test vectors: TEST ONLY, never an owner key. */ 27 const uint8_t cka_test1_pk[32] = { 28 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 29 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a}; 30 const uint8_t cka_test2_pk[32] = { 31 0x3d, 0x40, 0x17, 0xc3, 0xe8, 0x43, 0x89, 0x5a, 0x92, 0xb7, 0x0a, 0xa7, 0x4d, 0x1b, 0x7e, 0xbc, 32 0x9c, 0x98, 0x2c, 0xcf, 0x2e, 0xc4, 0x96, 0x8c, 0xc0, 0xcd, 0x55, 0xf1, 0x2a, 0xf4, 0x66, 0x0c}; 33 34 static const char *const err_names[21] = { 35 "Ok", "BadMagic", "UnsupportedVersion", "WrongTarget", "WrongAbi", "LengthOverflow", 36 "Truncated", "WrongLength", "ReservedNonZero", "UnsupportedFlags", "SectionOverlap", 37 "BadSection", "BadEntryPoint", "MalformedCapability", "ResourceLimit", "BadSignatureFormat", 38 "DigestMismatch", "UntrustedSigner", "BadSignature", "RightsEscalation", "MalformedReceipt"}; 39 static const char *const load_names[10] = { 40 "NoFrames", "StagingTooLarge", "Mapping", "MappedDigestMismatch", "WxAudit", 41 "CapabilityInstall", "SchedulerFull", "ExecutedDigestMismatch", "Reclaim", "FirmwareRead"}; 42 static const char *const stage_names[10] = {"none", "received", "staged", "verified", "authorized", 43 "reserved", "mapped", "hashed", "sealed", "caps"}; 44 45 const char *cka_error_name(unsigned code) 46 { 47 if (code < 21) 48 return err_names[code]; 49 if (code >= 0x101 && code <= 0x10a) 50 return load_names[code - 0x101]; 51 return "?"; 52 } 53 54 const char *cka_stage_name(unsigned stage) 55 { 56 return stage < 10 ? stage_names[stage] : "?"; 57 } 58 59 static int all_zero(const uint8_t *b, size_t n) 60 { 61 uint8_t acc = 0; 62 for (size_t i = 0; i < n; i++) 63 acc |= b[i]; 64 return acc == 0; 65 } 66 67 static int eq32(const uint8_t *a, const uint8_t *b) 68 { 69 uint8_t acc = 0; 70 for (int i = 0; i < 32; i++) 71 acc |= (uint8_t)(a[i] ^ b[i]); 72 return acc == 0; 73 } 74 75 void cka_digest_domain(const char *domain, size_t dlen, const uint8_t *b, size_t len, 76 uint8_t out[32]) 77 { 78 sha256_ctx c; 79 sha256_init(&c); 80 sha256_update(&c, (const uint8_t *)domain, dlen); 81 if (len) 82 sha256_update(&c, b, len); 83 sha256_final(&c, out); 84 } 85 86 /* ---- capability records (capability.rs) ---- */ 87 void cka_cap_encode(const struct cka_cap *c, uint8_t o[CKA_CAP_SIZE]) 88 { 89 for (unsigned i = 0; i < CKA_CAP_SIZE; i++) 90 o[i] = 0; 91 cka_wr16(o, c->kind); 92 cka_wr32(o + 4, c->id); 93 cka_wr32(o + 8, c->rights); 94 cka_wr32(o + 12, c->bounds); 95 cka_wr32(o + 16, c->max_ops); 96 cka_wr64(o + 24, c->max_bytes); 97 cka_wr64(o + 32, c->off); 98 cka_wr64(o + 40, c->len); 99 } 100 101 int cka_cap_validate(const struct cka_cap *c) 102 { 103 if ((c->kind != CKA_KIND_OBJECT && c->kind != CKA_KIND_CHANNEL) || c->id == 0 || 104 c->rights == 0 || (c->rights & ~CKA_VALID_RIGHTS) || c->max_ops == 0 || c->max_bytes == 0) 105 return CKA_MALFORMED_CAP; 106 if (c->kind == CKA_KIND_OBJECT && c->bounds == 1) { 107 if (c->off + c->len < c->off) 108 return CKA_LENGTH_OVERFLOW; 109 if (c->len == 0 || c->max_bytes > c->len) 110 return CKA_MALFORMED_CAP; 111 return CKA_OK; 112 } 113 if (c->kind == CKA_KIND_CHANNEL && c->bounds == 2) 114 return (c->off || c->len) ? CKA_MALFORMED_CAP : CKA_OK; 115 return CKA_MALFORMED_CAP; 116 } 117 118 int cka_cap_decode(const uint8_t b[CKA_CAP_SIZE], struct cka_cap *c) 119 { 120 if (cka_rd16(b + 2) || cka_rd32(b + 20)) 121 return CKA_RESERVED_NONZERO; 122 c->kind = cka_rd16(b); 123 c->id = cka_rd32(b + 4); 124 c->rights = cka_rd32(b + 8); 125 c->bounds = cka_rd32(b + 12); 126 c->max_ops = cka_rd32(b + 16); 127 c->max_bytes = cka_rd64(b + 24); 128 c->off = cka_rd64(b + 32); 129 c->len = cka_rd64(b + 40); 130 return cka_cap_validate(c); 131 } 132 133 int cka_cap_is_attenuation_of(const struct cka_cap *g, const struct cka_cap *r) 134 { 135 if (cka_cap_validate(g) || cka_cap_validate(r) || g->kind != r->kind || g->id != r->id || 136 g->bounds != r->bounds || (g->rights & ~r->rights) || g->max_ops > r->max_ops || 137 g->max_bytes > r->max_bytes) 138 return 0; 139 if (g->bounds == 1) 140 return g->off >= r->off && g->off + g->len <= r->off + r->len; 141 return 1; 142 } 143 144 /* ---- envelope (resource.rs) ---- */ 145 void cka_env_encode(const struct cka_env *e, uint8_t o[CKA_ENV_SIZE]) 146 { 147 for (unsigned i = 0; i < CKA_ENV_SIZE; i++) 148 o[i] = 0; 149 cka_wr32(o, e->code_pages); 150 cka_wr32(o + 4, e->data_pages); 151 cka_wr32(o + 8, e->stack_pages); 152 cka_wr16(o + 12, e->max_caps); 153 cka_wr32(o + 16, e->ipc_msgs); 154 cka_wr32(o + 20, e->ipc_bytes); 155 cka_wr64(o + 24, e->cpu); 156 cka_wr64(o + 32, e->elapsed); 157 cka_wr32(o + 40, e->syscalls); 158 } 159 160 int cka_env_validate(const struct cka_env *e, unsigned ncaps) 161 { 162 if (e->code_pages < 1 || e->code_pages > 64 || e->data_pages < 1 || e->data_pages > 64 || 163 e->stack_pages < 1 || e->stack_pages > 16 || ncaps > e->max_caps || 164 e->max_caps > CKA_MAX_CAPS || e->ipc_msgs > 1024 || e->ipc_bytes > 1048576u || 165 e->cpu < 1 || e->cpu > 1000000000ull || e->elapsed < 1 || e->elapsed > 1000000000ull || 166 e->syscalls < 1 || e->syscalls > 65536) 167 return CKA_RESOURCE_LIMIT; 168 return CKA_OK; 169 } 170 171 int cka_env_validate_profile(const struct cka_env *e, unsigned ncaps, uint16_t target, uint16_t abi) 172 { 173 if (target == CKA_TARGET_X86_64_LE && abi == CKA_ABI_OMEGA_X86_MODEL_V1) { 174 if (ncaps || e->max_caps || e->ipc_msgs || e->ipc_bytes || e->code_pages != 32 || 175 e->data_pages != 1024 || e->stack_pages != 16 || e->cpu != `120000000` || 176 e->elapsed != `180000000` || e->syscalls != 1) 177 return CKA_RESOURCE_LIMIT; 178 return CKA_OK; 179 } 180 return cka_env_validate(e, ncaps); 181 } 182 183 static int env_decode(const uint8_t *b, unsigned ncaps, struct cka_env *e, uint16_t target, uint16_t abi) 184 { 185 if (cka_rd16(b + 14) || cka_rd32(b + 44)) 186 return CKA_RESERVED_NONZERO; 187 e->code_pages = cka_rd32(b); 188 e->data_pages = cka_rd32(b + 4); 189 e->stack_pages = cka_rd32(b + 8); 190 e->max_caps = cka_rd16(b + 12); 191 e->ipc_msgs = cka_rd32(b + 16); 192 e->ipc_bytes = cka_rd32(b + 20); 193 e->cpu = cka_rd64(b + 24); 194 e->elapsed = cka_rd64(b + 32); 195 e->syscalls = cka_rd32(b + 40); 196 return cka_env_validate_profile(e, ncaps, target, abi); 197 } 198 199 /* ---- parse (format.rs) ---- */ 200 static void section_read(const uint8_t *b, struct cka_section *s) 201 { 202 s->kind = cka_rd16(b); 203 s->perms = cka_rd16(b + 2); 204 s->rel = cka_rd32(b + 8); 205 s->flen = cka_rd32(b + 12); 206 s->mlen = cka_rd32(b + 16); 207 s->align = cka_rd32(b + 20); 208 } 209 210 int cka_parse_profile(const uint8_t *b, size_t len, struct cka_parsed *p, 211 uint16_t target, uint16_t abi) 212 { 213 static const uint8_t magic[8] = {'A', 'I', 'E', 'N', 'A', 'R', 'T', 0}; 214 if (len < CKA_HEADER_SIZE) 215 return CKA_TRUNCATED; 216 if (len > CKA_MAX_ARTIFACT) 217 return CKA_RESOURCE_LIMIT; 218 for (int i = 0; i < 8; i++) 219 if (b[i] != magic[i]) 220 return CKA_BAD_MAGIC; 221 if (cka_rd16(b + 8) != 0 || cka_rd16(b + 10) != CKA_HEADER_SIZE) 222 return CKA_UNSUPPORTED_VERSION; 223 p->target = cka_rd16(b + 12); 224 p->abi = cka_rd16(b + 14); 225 if (p->target != target) 226 return CKA_WRONG_TARGET; 227 if (p->abi != abi) 228 return CKA_WRONG_ABI; 229 if (cka_rd32(b + 16) != 0) 230 return CKA_UNSUPPORTED_FLAGS; 231 p->total = cka_rd32(b + 20); 232 if (p->total != len) 233 return CKA_WRONG_LENGTH; 234 if (cka_rd32(b + 24) != CKA_HEADER_SIZE || cka_rd16(b + 28) != 2 || 235 cka_rd16(b + 30) != CKA_SECTION_SIZE || cka_rd16(b + 32) || cka_rd16(b + 34)) 236 return CKA_UNSUPPORTED_VERSION; 237 p->entry = cka_rd32(b + 36); 238 if (cka_rd32(b + 40) != CKA_CAPS_OFFSET || cka_rd16(b + 46) != CKA_CAP_SIZE || 239 cka_rd16(b + 52) != CKA_ENV_SIZE || cka_rd16(b + 54) != 0) 240 return CKA_UNSUPPORTED_VERSION; 241 if (!all_zero(b + 72, CKA_HEADER_SIZE - 72)) 242 return CKA_RESERVED_NONZERO; 243 p->ncaps = cka_rd16(b + 44); 244 if (p->ncaps > CKA_MAX_CAPS) 245 return CKA_RESOURCE_LIMIT; 246 p->res_off = cka_rd32(b + 48); 247 if (p->res_off != CKA_CAPS_OFFSET + CKA_CAP_SIZE * p->ncaps) 248 return CKA_SECTION_OVERLAP; 249 uint32_t env_end = p->res_off + CKA_ENV_SIZE; 250 p->payload_off = cka_rd32(b + 56); 251 if (p->payload_off != ((env_end + 15u) & ~15u)) 252 return CKA_SECTION_OVERLAP; 253 /* total == len >= 128 here; the gap lies inside the file only if it fits */ 254 if ((uint64_t)p->payload_off > len) 255 return CKA_WRONG_LENGTH; 256 if (!all_zero(b + env_end, p->payload_off - env_end)) 257 return CKA_RESERVED_NONZERO; 258 p->payload_len = cka_rd32(b + 60); 259 if (p->payload_len > CKA_MAX_PAYLOAD) 260 return CKA_RESOURCE_LIMIT; 261 if (cka_rd16(b + 68) != CKA_SIG_SIZE || cka_rd16(b + 70) != 1) 262 return CKA_BAD_SIG_FORMAT; 263 p->sig_off = cka_rd32(b + 64); 264 if ((uint64_t)p->sig_off != (uint64_t)p->payload_off + p->payload_len) 265 return CKA_SECTION_OVERLAP; 266 if ((uint64_t)p->sig_off + CKA_SIG_SIZE != p->total) 267 return CKA_WRONG_LENGTH; 268 const uint8_t *s0 = b + 128, *s1 = b + 160; 269 if (cka_rd32(s0 + 4) || !all_zero(s0 + 24, 8) || cka_rd32(s1 + 4) || !all_zero(s1 + 24, 8)) 270 return CKA_RESERVED_NONZERO; 271 section_read(s0, &p->code); 272 section_read(s1, &p->data); 273 const struct cka_section *c = &p->code, *d = &p->data; 274 if (c->kind != 1 || c->perms != 5 || c->rel != 0 || c->flen == 0 || c->mlen != c->flen || 275 c->align != 4096) 276 return CKA_BAD_SECTION; 277 if (d->kind != 2 || d->perms != 3 || d->rel != c->flen || d->flen == 0 || d->mlen < d->flen || 278 d->align != 4096) 279 return CKA_BAD_SECTION; 280 if ((uint64_t)c->flen + d->flen != p->payload_len) 281 return CKA_SECTION_OVERLAP; 282 for (unsigned i = 0; i < p->ncaps; i++) { 283 int rc = cka_cap_decode(b + CKA_CAPS_OFFSET + CKA_CAP_SIZE * i, &p->caps[i]); 284 if (rc) 285 return rc; 286 } 287 for (unsigned i = 1; i < p->ncaps; i++) { 288 const struct cka_cap *a = &p->caps[i - 1], *z = &p->caps[i]; 289 if (!(a->kind < z->kind || (a->kind == z->kind && a->id < z->id))) 290 return CKA_MALFORMED_CAP; 291 } 292 int rc = env_decode(b + p->res_off, p->ncaps, &p->env, target, abi); 293 if (rc) 294 return rc; 295 if ((uint64_t)c->mlen > (uint64_t)p->env.code_pages * 4096u || 296 (uint64_t)d->mlen > (uint64_t)p->env.data_pages * 4096u) 297 return CKA_RESOURCE_LIMIT; 298 if ((target == CKA_TARGET_AARCH64_LE && ((p->entry & 3) || (uint64_t)p->entry + 4 > c->flen)) || 299 p->entry >= c->flen) 300 return CKA_BAD_ENTRY; 301 const uint8_t *sg = b + p->sig_off; 302 p->sig_alg = cka_rd16(sg); 303 if (p->sig_alg != 1) 304 return CKA_BAD_SIG_FORMAT; 305 if (sg[2] || sg[3]) 306 return CKA_RESERVED_NONZERO; 307 if (p->sig_alg != cka_rd16(b + 70)) 308 return CKA_BAD_SIG_FORMAT; 309 for (int i = 0; i < 32; i++) 310 p->fp[i] = sg[4 + i]; 311 for (int i = 0; i < 64; i++) 312 p->sig[i] = sg[36 + i]; 313 return CKA_OK; 314 } 315 316 int cka_identify_profile(const uint8_t *b, size_t len, struct cka_parsed *p, struct cka_ident *id, 317 uint16_t target, uint16_t abi) 318 { 319 if (len > CKA_MAX_ARTIFACT) 320 return CKA_RESOURCE_LIMIT; 321 int rc = cka_parse_profile(b, len, p, target, abi); 322 if (rc) 323 return rc; 324 cka_digest_domain(DOM("AIENOS-ARTIFACT-V1"), b, p->sig_off, id->id); 325 sha256_hash(b + p->payload_off, p->payload_len, id->payload); 326 cka_digest_domain(DOM("AIENOS-ARTIFACT-CAP-REQUESTS-V1"), b + CKA_CAPS_OFFSET, 327 (size_t)CKA_CAP_SIZE * p->ncaps, id->requested); 328 uint8_t env[CKA_ENV_SIZE]; 329 cka_env_encode(&p->env, env); 330 cka_digest_domain(DOM("AIENOS-ARTIFACT-RESOURCES-V1"), env, sizeof env, id->resources); 331 return CKA_OK; 332 } 333 334 void cka_signature_message(const uint8_t id[32], uint8_t *msg, size_t *len) 335 { 336 static const char dom[] = "AIENOS-ARTIFACT-SIGNATURE-V1"; 337 size_t n = sizeof dom; 338 for (size_t i = 0; i < n; i++) 339 msg[i] = (uint8_t)dom[i]; 340 for (int i = 0; i < 32; i++) 341 msg[n + i] = id[i]; 342 *len = n + 32; 343 } 344 345 int cka_verify_profile(const uint8_t *b, size_t len, const uint8_t (*anchors)[32], unsigned nanchors, 346 struct cka_parsed *p, struct cka_ident *id, uint8_t signer_fp[32], 347 uint16_t target, uint16_t abi) 348 { 349 int rc = cka_identify_profile(b, len, p, id, target, abi); 350 if (rc) 351 return rc; 352 const uint8_t *key = 0; 353 uint8_t fp[32]; 354 for (unsigned i = 0; i < nanchors && !key; i++) { 355 sha256_hash(anchors[i], 32, fp); 356 if (eq32(fp, p->fp)) 357 key = anchors[i]; 358 } 359 if (!key) 360 return CKA_UNTRUSTED_SIGNER; 361 sha256_hash(key, 32, fp); 362 if (!eq32(fp, p->fp)) 363 return CKA_UNTRUSTED_SIGNER; 364 uint8_t msg[96]; 365 size_t mlen; 366 cka_signature_message(id->id, msg, &mlen); 367 if (aienos_`ed25519`_verify(p->sig, msg, mlen, key) != 0) 368 return CKA_BAD_SIGNATURE; 369 for (int i = 0; i < 32; i++) 370 signer_fp[i] = p->fp[i]; 371 return CKA_OK; 372 } 373 374 void cka_grants_digest(const struct cka_cap *g, unsigned n, uint8_t out[32]) 375 { 376 sha256_ctx c; 377 static const char dom[] = "AIENOS-ADMISSION-CAP-GRANTS-V1"; 378 uint8_t rec[CKA_CAP_SIZE]; 379 sha256_init(&c); 380 sha256_update(&c, (const uint8_t *)dom, sizeof dom); 381 for (unsigned i = 0; i < n; i++) { 382 cka_cap_encode(&g[i], rec); 383 sha256_update(&c, rec, sizeof rec); 384 } 385 sha256_final(&c, out); 386 } 387 388 /* Existing AArch64 callers keep their original, closed target and ABI. */ 389 int cka_parse(const uint8_t *b, size_t n, struct cka_parsed *p) 390 { return cka_parse_profile(b, n, p, CKA_TARGET_AARCH64_LE, CKA_ABI_V1); } 391 int cka_identify(const uint8_t *b, size_t n, struct cka_parsed *p, struct cka_ident *id) 392 { return cka_identify_profile(b, n, p, id, CKA_TARGET_AARCH64_LE, CKA_ABI_V1); } 393 int cka_verify(const uint8_t *b, size_t n, const uint8_t (*a)[32], unsigned na, 394 struct cka_parsed *p, struct cka_ident *id, uint8_t fp[32]) 395 { return cka_verify_profile(b, n, a, na, p, id, fp, CKA_TARGET_AARCH64_LE, CKA_ABI_V1); }