aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / artifact / ck_artifact.h
| 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 | /* ck_artifact.h -- SEED-0B sealed artifact format, admission policy and |
| 18 | * admission receipts in C (port of crates/aienos-artifact: format.rs, |
| 19 | * capability.rs, resource.rs, verify.rs, admission.rs, receipt.rs and the |
| 20 | * boot policy of aienos-kernel artifact_loader.rs). Pure code: no kernel |
| 21 | * state, builds freestanding (kernel) and hosted (host tests, tool). |
| 22 | * Error codes, stage codes and byte layouts are the Rust ones (ADR 0014). */ |
| 23 | #ifndef AIENOS_CK_ARTIFACT_H |
| 24 | #define AIENOS_CK_ARTIFACT_H |
| 25 | |
| 26 | #include <stddef.h> |
| 27 | #include <stdint.h> |
| 28 | |
| 29 | /* ---- error codes (ArtifactError, receipt reason codes 1..20) ---- */ |
| 30 | enum { |
| 31 | CKA_OK = 0, |
| 32 | CKA_BAD_MAGIC = 1, |
| 33 | CKA_UNSUPPORTED_VERSION = 2, |
| 34 | CKA_WRONG_TARGET = 3, |
| 35 | CKA_WRONG_ABI = 4, |
| 36 | CKA_LENGTH_OVERFLOW = 5, |
| 37 | CKA_TRUNCATED = 6, |
| 38 | CKA_WRONG_LENGTH = 7, |
| 39 | CKA_RESERVED_NONZERO = 8, |
| 40 | CKA_UNSUPPORTED_FLAGS = 9, |
| 41 | CKA_SECTION_OVERLAP = 10, |
| 42 | CKA_BAD_SECTION = 11, |
| 43 | CKA_BAD_ENTRY = 12, |
| 44 | CKA_MALFORMED_CAP = 13, |
| 45 | CKA_RESOURCE_LIMIT = 14, |
| 46 | CKA_BAD_SIG_FORMAT = 15, |
| 47 | CKA_DIGEST_MISMATCH = 16, |
| 48 | CKA_UNTRUSTED_SIGNER = 17, |
| 49 | CKA_BAD_SIGNATURE = 18, |
| 50 | CKA_RIGHTS_ESCALATION = 19, |
| 51 | CKA_MALFORMED_RECEIPT = 20, |
| 52 | }; |
| 53 | /* LoadError reason codes (receipt reason 0x101..0x10a). */ |
| 54 | enum { |
| 55 | CKL_NO_FRAMES = 0x101, |
| 56 | CKL_STAGING_TOO_LARGE = 0x102, |
| 57 | CKL_MAPPING = 0x103, |
| 58 | CKL_MAPPED_DIGEST = 0x104, |
| 59 | CKL_WX_AUDIT = 0x105, |
| 60 | CKL_CAP_INSTALL = 0x106, |
| 61 | CKL_SCHEDULER_FULL = 0x107, |
| 62 | CKL_EXECUTED_DIGEST = 0x108, |
| 63 | CKL_RECLAIM = 0x109, |
| 64 | CKL_FIRMWARE_READ = 0x10a, |
| 65 | }; |
| 66 | /* Rejection stages (receipt stage 1..9). */ |
| 67 | enum { |
| 68 | CKS_RECEIVED = 1, CKS_STAGED, CKS_VERIFIED, CKS_AUTHORIZED, CKS_RESERVED, |
| 69 | CKS_MAPPED, CKS_HASHED, CKS_SEALED, CKS_CAPS, |
| 70 | }; |
| 71 | const char *cka_error_name(unsigned code); /* Rust Debug name, "?" if unknown */ |
| 72 | const char *cka_stage_name(unsigned stage); |
| 73 | |
| 74 | #define CKA_HEADER_SIZE 128u |
| 75 | #define CKA_SECTION_SIZE 32u |
| 76 | #define CKA_CAP_SIZE 48u |
| 77 | #define CKA_ENV_SIZE 48u |
| 78 | #define CKA_SIG_SIZE 100u |
| 79 | #define CKA_MAX_CAPS 16u |
| 80 | #define CKA_MAX_ARTIFACT (16u << 20) |
| 81 | #define CKA_MAX_PAYLOAD (8u << 20) |
| 82 | #define CKA_CAPS_OFFSET 192u |
| 83 | #define CKA_TARGET_AARCH64_LE 1u |
| 84 | #define CKA_ABI_V1 1u |
| 85 | #define CKA_TARGET_X86_64_LE 2u |
| 86 | #define CKA_ABI_OMEGA_X86_LOCAL_V1 2u |
| 87 | /* Closed model experiment: four args, immutable external weights, 4 MiB scratch. */ |
| 88 | #define CKA_ABI_OMEGA_X86_MODEL_V1 3u |
| 89 | #define CKA_KIND_CHANNEL 2u |
| 90 | #define CKA_KIND_OBJECT 3u |
| 91 | #define CKA_VALID_RIGHTS 0x3fu |
| 92 | |
| 93 | struct cka_cap { |
| 94 | uint16_t kind; |
| 95 | uint32_t id, rights, bounds, max_ops; |
| 96 | uint64_t max_bytes, off, len; |
| 97 | }; |
| 98 | struct cka_env { |
| 99 | uint32_t code_pages, data_pages, stack_pages; |
| 100 | uint16_t max_caps; |
| 101 | uint32_t ipc_msgs, ipc_bytes; |
| 102 | uint64_t cpu, elapsed; |
| 103 | uint32_t syscalls; |
| 104 | }; |
| 105 | struct cka_section { |
| 106 | uint16_t kind, perms; |
| 107 | uint32_t rel, flen, mlen, align; |
| 108 | }; |
| 109 | struct cka_parsed { |
| 110 | uint16_t target, abi, ncaps, sig_alg; |
| 111 | uint32_t total, entry, res_off, payload_off, payload_len, sig_off; |
| 112 | struct cka_section code, data; |
| 113 | struct cka_cap caps[CKA_MAX_CAPS]; |
| 114 | struct cka_env env; |
| 115 | uint8_t fp[32]; |
| 116 | uint8_t sig[64]; |
| 117 | }; |
| 118 | struct cka_ident { |
| 119 | uint8_t id[32], payload[32], requested[32], resources[32]; |
| 120 | }; |
| 121 | |
| 122 | void cka_cap_encode(const struct cka_cap *c, uint8_t out[CKA_CAP_SIZE]); |
| 123 | int cka_cap_decode(const uint8_t b[CKA_CAP_SIZE], struct cka_cap *c); |
| 124 | int cka_cap_validate(const struct cka_cap *c); |
| 125 | int cka_cap_is_attenuation_of(const struct cka_cap *g, const struct cka_cap *r); |
| 126 | void cka_env_encode(const struct cka_env *e, uint8_t out[CKA_ENV_SIZE]); |
| 127 | int cka_env_validate_profile(const struct cka_env *, unsigned, uint16_t, uint16_t); |
| 128 | int cka_env_validate(const struct cka_env *e, unsigned ncaps); |
| 129 | |
| 130 | /* format.rs parse; parse_and_identify adds the 16 MiB bound and the digests. */ |
| 131 | int cka_parse(const uint8_t *b, size_t len, struct cka_parsed *p); |
| 132 | int cka_identify(const uint8_t *b, size_t len, struct cka_parsed *p, struct cka_ident *id); |
| 133 | /* verify.rs: anchors are TEST/public keys (32 bytes each). Fills signer_fp. */ |
| 134 | int cka_verify(const uint8_t *b, size_t len, const uint8_t (*anchors)[32], unsigned nanchors, |
| 135 | struct cka_parsed *p, struct cka_ident *id, uint8_t signer_fp[32]); |
| 136 | void cka_signature_message(const uint8_t id[32], uint8_t *msg, size_t *len); /* msg >= 64 */ |
| 137 | |
| 138 | /* Explicit consumer profile; never inferred from untrusted artifact bytes. */ |
| 139 | int cka_parse_profile(const uint8_t *, size_t, struct cka_parsed *, uint16_t, uint16_t); |
| 140 | int cka_identify_profile(const uint8_t *, size_t, struct cka_parsed *, struct cka_ident *, uint16_t, uint16_t); |
| 141 | int cka_verify_profile(const uint8_t *, size_t, const uint8_t (*)[32], unsigned, |
| 142 | struct cka_parsed *, struct cka_ident *, uint8_t [32], uint16_t, uint16_t); |
| 143 | /* ---- digests ---- */ |
| 144 | void cka_digest_domain(const char *domain, size_t dlen, const uint8_t *b, size_t len, |
| 145 | uint8_t out[32]); |
| 146 | void cka_grants_digest(const struct cka_cap *g, unsigned n, uint8_t out[32]); |
| 147 | |
| 148 | /* ---- admission policy (admission.rs) ---- */ |
| 149 | #define CKA_POLICY_MAX_RULES 4u |
| 150 | #define CKA_POLICY_MAX_SIGNERS 4u |
| 151 | struct cka_limits { |
| 152 | uint32_t code_pages, data_pages, stack_pages, max_caps, ipc_msgs, ipc_bytes; |
| 153 | uint64_t cpu, elapsed; |
| 154 | uint32_t syscalls; |
| 155 | }; |
| 156 | struct cka_policy { |
| 157 | struct cka_limits limits; |
| 158 | unsigned nrules, nsigners; |
| 159 | struct cka_cap rules[CKA_POLICY_MAX_RULES]; |
| 160 | uint8_t signers[CKA_POLICY_MAX_SIGNERS][32]; |
| 161 | }; |
| 162 | /* The boot policy; with a TEST anchor the signer list is SHA256(anchor). */ |
| 163 | void cka_boot_policy(struct cka_policy *pol, const uint8_t (*anchors)[32], unsigned nanchors); |
| 164 | size_t cka_policy_canonical(const struct cka_policy *pol, uint8_t *out, size_t cap); |
| 165 | void cka_policy_digest(const struct cka_policy *pol, uint8_t out[32]); |
| 166 | struct cka_decision { |
| 167 | unsigned ngrants; |
| 168 | struct cka_cap grants[CKA_MAX_CAPS]; |
| 169 | uint8_t granted_digest[32]; |
| 170 | }; |
| 171 | /* evaluate(): avail = available resources of the machine. */ |
| 172 | int cka_evaluate(const struct cka_policy *pol, const struct cka_parsed *p, |
| 173 | const uint8_t signer_fp[32], const struct cka_limits *avail, |
| 174 | struct cka_decision *d); |
| 175 | |
| 176 | /* ---- admission receipt (receipt.rs) ---- */ |
| 177 | #define CKA_RECEIPT_SIZE 512u |
| 178 | #define CKA_RECEIPT_SIGNED_LEN 400u |
| 179 | enum { CKR_ADMITTED = 1, CKR_REJECTED = 2 }; |
| 180 | enum { CKR_TIER_SEED0B_QEMU = 1, CKR_TIER_SEED0B_PI5 = 3 }; |
| 181 | /* Tier a receipt carries, chosen by the image: the Pi image (CK_PI5) writes SEED-0B-PI5, every other image |
| 182 | * (QEMU, Spark) writes SEED-0B-QEMU exactly as before. CKA_TIER_MUTANT is a host-test mutant only. */ |
| 183 | #if defined(CK_PI5) && !defined(CKA_TIER_MUTANT) |
| 184 | #define CKA_IMAGE_TIER CKR_TIER_SEED0B_PI5 |
| 185 | #else |
| 186 | #define CKA_IMAGE_TIER CKR_TIER_SEED0B_QEMU |
| 187 | #endif |
| 188 | /* Tiers decode accepts: the Pi tier only in the Pi image and in hosted tools (the verifier); the QEMU and |
| 189 | * Spark kernels still accept SEED-0B-QEMU only, so their code is unchanged. */ |
| 190 | #if defined(CK_PI5) || (defined(__STDC_HOSTED__) && __STDC_HOSTED__) |
| 191 | #define CKA_TIER_OK(t) ((t) == CKR_TIER_SEED0B_QEMU || (t) == CKR_TIER_SEED0B_PI5) |
| 192 | #else |
| 193 | #define CKA_TIER_OK(t) ((t) == CKR_TIER_SEED0B_QEMU) |
| 194 | #endif |
| 195 | enum { |
| 196 | CKR_NOT_RUN = 0, CKR_EXITED, CKR_TIMEOUT, CKR_FAULT, CKR_BAD_SYSCALL, CKR_RESOURCE_OVERRUN, |
| 197 | }; |
| 198 | #define CKR_F_READ_OK 0x01u |
| 199 | #define CKR_F_WRITE_DENIED 0x02u |
| 200 | #define CKR_F_RECLAIMED 0x04u |
| 201 | #define CKR_F_CANARY 0x08u |
| 202 | #define CKR_F_FORGED_DENIED 0x10u |
| 203 | #define CKR_F_MAPPED 0x20u |
| 204 | #define CKR_F_EXECUTED 0x40u |
| 205 | #define CKR_F_WX 0x80u |
| 206 | struct cka_receipt { |
| 207 | uint32_t flags; |
| 208 | uint16_t decision, tier; |
| 209 | uint64_t seq; |
| 210 | uint8_t nonce[16]; |
| 211 | uint64_t time; |
| 212 | uint32_t status; |
| 213 | int32_t exit; |
| 214 | uint32_t result_flags; |
| 215 | uint16_t stage, reason; |
| 216 | uint32_t syscalls, reads, denials, frames; |
| 217 | uint8_t id[32], payload[32], signer[32], policy[32], requested[32], granted[32], |
| 218 | resources[32], verifier[32], machine[32]; |
| 219 | uint64_t generation, context; |
| 220 | uint16_t sig_alg; |
| 221 | uint8_t fp[32], sig[64]; |
| 222 | }; |
| 223 | const char *cka_status_name(uint32_t status); |
| 224 | void cka_receipt_nonce(const uint8_t verifier[32], uint64_t seq, uint8_t out[16]); |
| 225 | void cka_receipt_encode(const struct cka_receipt *r, uint8_t out[CKA_RECEIPT_SIZE]); |
| 226 | int cka_receipt_decode(const uint8_t b[CKA_RECEIPT_SIZE], struct cka_receipt *r); |
| 227 | int cka_receipt_validate(const struct cka_receipt *r); |
| 228 | void cka_receipt_digest(const uint8_t b[CKA_RECEIPT_SIZE], uint8_t out[32]); |
| 229 | int cka_receipt_is_signed(const uint8_t b[CKA_RECEIPT_SIZE]); |
| 230 | /* Signs an unsigned record in place with an Ed25519 seed (TEST key only). */ |
| 231 | int cka_receipt_sign(uint8_t b[CKA_RECEIPT_SIZE], const uint8_t seed[32]); |
| 232 | int cka_receipt_verify(const uint8_t b[CKA_RECEIPT_SIZE], const uint8_t (*anchors)[32], |
| 233 | unsigned nanchors); |
| 234 | /* Verifier identity of a build (commit = 40 lowercase hex, else "unpinned-build"). */ |
| 235 | void cka_verifier_identity(const char *commit, uint8_t feature, uint8_t out[32]); |
| 236 | |
| 237 | /* ---- TEST-ONLY qualification keys (RFC 8032 test vectors 1 and 2) ---- */ |
| 238 | extern const uint8_t cka_test1_pk[32]; |
| 239 | extern const uint8_t cka_test2_pk[32]; |
| 240 | |
| 241 | /* little-endian field helpers */ |
| 242 | static inline uint16_t cka_rd16(const uint8_t *b) { return (uint16_t)(b[0] | b[1] << 8); } |
| 243 | static inline uint32_t cka_rd32(const uint8_t *b) |
| 244 | { |
| 245 | return (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24; |
| 246 | } |
| 247 | static inline uint64_t cka_rd64(const uint8_t *b) |
| 248 | { |
| 249 | return (uint64_t)cka_rd32(b) | (uint64_t)cka_rd32(b + 4) << 32; |
| 250 | } |
| 251 | static inline void cka_wr16(uint8_t *b, uint16_t v) { b[0] = (uint8_t)v; b[1] = (uint8_t)(v >> 8); } |
| 252 | static inline void cka_wr32(uint8_t *b, uint32_t v) |
| 253 | { |
| 254 | for (int i = 0; i < 4; i++) |
| 255 | b[i] = (uint8_t)(v >> (8 * i)); |
| 256 | } |
| 257 | static inline void cka_wr64(uint8_t *b, uint64_t v) |
| 258 | { |
| 259 | for (int i = 0; i < 8; i++) |
| 260 | b[i] = (uint8_t)(v >> (8 * i)); |
| 261 | } |
| 262 | |
| 263 | #endif |