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/osc_receipt.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 / osc_receipt.c` 223 lines ยท 9160 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_receipt.c -- Omega realization receipt v1 verification. See osc_receipt.h and omega 18 * spec/one-layer-receipts.md. Written from the spec: section 2 (layout), 3 (signature), 5 (checker identity), 19 * 6 (order and reasons). One comment per step of section 6; the first failure decides the result. 20 * 21 * MUT(n) marks a guard the host mutation run (Makefile osc-receipt-mutants) removes, one at a time: with 22 * -DOSC_RCPT_MUTANT=n guard n is skipped and tests/test_osc_receipt.c must fail. Mutants build only with 23 * OSC_RCPT_HOST_TEST; a kernel build refuses the flag. */ 24 #include 25 #include "osc_receipt.h" 26 #include "osc_receipt_anchor.h" 27 #include "aienos_sig.h" 28 #include "sha256.h" 29 30 #ifdef OSC_RCPT_MUTANT 31 #ifndef OSC_RCPT_HOST_TEST 32 #error "receipt mutants are TEST-only host builds (OSC_RCPT_HOST_TEST)" 33 #endif 34 #define MUT(n) (OSC_RCPT_MUTANT == (n)) 35 #else 36 #define MUT(n) 0 37 #endif 38 39 static uint16_t rd16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } 40 static uint64_t rd64(const uint8_t *p) 41 { 42 uint64_t v = 0; 43 for (int i = 7; i >= 0; i--) 44 v = (v << 8) | p[i]; 45 return v; 46 } 47 48 /* constant time over the 32 bytes (keys, hashes, identities; AGENTS.md section 6) */ 49 static int eq32(const uint8_t *a, const uint8_t *b) 50 { 51 uint8_t d = 0; 52 for (unsigned i = 0; i < 32; i++) 53 d |= (uint8_t)(a[i] ^ b[i]); 54 return d == 0; 55 } 56 57 static int in_list(const uint8_t *v, const uint8_t (*list)[32], unsigned n) 58 { 59 int hit = 0; 60 for (unsigned i = 0; list && i < n; i++) 61 hit |= eq32(v, list[i]); 62 return hit; 63 } 64 65 /* Index of the anchor whose SHA-256 is fp, or -1. */ 66 static int anchor_by_fp(const uint8_t *fp, const uint8_t (*anchors)[32], unsigned n) 67 { 68 int found = -1; 69 for (unsigned i = 0; anchors && i < n; i++) { 70 uint8_t h[32]; 71 sha256_hash(anchors[i], 32, h); 72 if (eq32(h, fp) && found < 0) 73 found = (int)i; 74 } 75 return found; 76 } 77 78 void osc_receipt_target_hash(const char *name, size_t name_len, uint8_t out[32]) 79 { 80 static const char dom[] = "OMEGA-TARGET-V1"; /* + its terminating NUL (16 bytes) */ 81 sha256_ctx c; 82 sha256_init(&c); 83 sha256_update(&c, (const uint8_t *)dom, sizeof dom); 84 sha256_update(&c, (const uint8_t *)name, name_len); 85 sha256_final(&c, out); 86 } 87 88 /* SHA-256("OMEGA-RECEIPT-DEPS-V1\0" || u32 count = 0): the empty dependency list (spec section 2, offset 112). */ 89 static void empty_deps(uint8_t out[32]) 90 { 91 static const char dom[] = "OMEGA-RECEIPT-DEPS-V1"; 92 static const uint8_t zero_count[4] = { 0, 0, 0, 0 }; 93 sha256_ctx c; 94 sha256_init(&c); 95 sha256_update(&c, (const uint8_t *)dom, sizeof dom); 96 sha256_update(&c, zero_count, sizeof zero_count); 97 sha256_final(&c, out); 98 } 99 100 static const uint8_t zero32[32]; 101 102 unsigned osc_receipt_verify(const uint8_t *r, size_t len, const struct osc_accept *a, const struct osc_policy *p, 103 struct osc_receipt_info *info) 104 { 105 struct osc_receipt_info local; 106 if (!info) 107 info = &local; 108 memset(info, 0, sizeof *info); 109 if (!r || len == 0) 110 return OSC_RCPT_MISSING; 111 if (!a || !p) 112 return OSC_RCPT_ARGUMENT; 113 114 /* 1. Structure: SIZE, MAGIC, VERSION, SIGNATURE_ALGORITHM, SIGNER_CLASS, EVIDENCE_CLASS, EPOCH_ORDER */ 115 if (!MUT(1) && len != OSC_RCPT_BYTES) 116 return OSC_RCPT_SIZE; 117 sha256_hash(r, OSC_RCPT_BYTES, info->id); 118 info->have_id = 1; 119 if (!MUT(2) && memcmp(r, "OMRCPT1\0", 8) != 0) 120 return OSC_RCPT_MAGIC; 121 if (!MUT(3) && rd16(r + 8) != 1) 122 return OSC_RCPT_VERSION; 123 unsigned cls = rd16(r + 10), alg = rd16(r + 12), ev = rd16(r + 14); 124 info->signer_class = (uint16_t)cls; 125 info->evidence_class = (uint16_t)ev; 126 info->policy_epoch = rd64(r + 304); 127 info->expiry_epoch = rd64(r + 312); 128 if (!MUT(4) && alg != 1) 129 return OSC_RCPT_SIGNATURE_ALGORITHM; 130 if (!MUT(5) && cls != OSC_SIGNER_TEST && cls != OSC_SIGNER_OWNER) 131 return OSC_RCPT_SIGNER_CLASS; 132 if (!MUT(6) && ev != 1 && ev != 2) 133 return OSC_RCPT_EVIDENCE_CLASS; 134 if (!MUT(7) && info->expiry_epoch < info->policy_epoch) 135 return OSC_RCPT_EPOCH_ORDER; 136 137 /* 2. Class policy. Release mode is the spec's production mode: only OWNER receipts. The key comes from the 138 * verifier's own anchor set for the receipt's class, matched by fingerprint, never from the receipt. In 139 * release mode the only owner anchor honoured is the listed owner root; a key anchored in both classes is 140 * refused. */ 141 if (!MUT(8) && p->release && cls != OSC_SIGNER_OWNER) 142 return OSC_RCPT_TEST_SIGNER_IN_PRODUCTION; 143 const uint8_t *fp = r + 320; 144 const uint8_t (*mine)[32] = cls == OSC_SIGNER_TEST ? p->rcpt_test_anchors : p->rcpt_owner_anchors; 145 unsigned n_mine = cls == OSC_SIGNER_TEST ? p->n_rcpt_test : p->n_rcpt_owner; 146 int k = anchor_by_fp(fp, mine, n_mine); 147 if (!MUT(9) && k < 0) 148 return OSC_RCPT_UNTRUSTED_SIGNER; 149 if (k < 0) /* only reachable under mutant 9: there is no key to verify with */ 150 return OSC_RCPT_BAD_SIGNATURE; 151 const uint8_t *pk = mine[k]; 152 if (!MUT(10) && p->release && cls == OSC_SIGNER_OWNER && !eq32(pk, osc_rcpt_owner_root_pk)) 153 return OSC_RCPT_UNTRUSTED_SIGNER; 154 { 155 const uint8_t (*other)[32] = cls == OSC_SIGNER_TEST ? p->rcpt_owner_anchors : p->rcpt_test_anchors; 156 unsigned n_other = cls == OSC_SIGNER_TEST ? p->n_rcpt_owner : p->n_rcpt_test; 157 if (!MUT(11) && in_list(pk, other, n_other)) 158 return OSC_RCPT_UNTRUSTED_SIGNER; 159 } 160 161 /* 3. Signature over "OMEGA-REALIZATION-RECEIPT-SIG-V1\0" || SHA-256("OMEGA-REALIZATION-RECEIPT-V1\0" || [0,352)) */ 162 { 163 static const char body_dom[] = "OMEGA-REALIZATION-RECEIPT-V1"; 164 static const char sig_dom[] = "OMEGA-REALIZATION-RECEIPT-SIG-V1"; 165 uint8_t msg[sizeof sig_dom + 32]; 166 sha256_ctx c; 167 memcpy(msg, sig_dom, sizeof sig_dom); 168 sha256_init(&c); 169 sha256_update(&c, (const uint8_t *)body_dom, sizeof body_dom); 170 sha256_update(&c, r, OSC_RCPT_SIGNED_BYTES); 171 sha256_final(&c, msg + sizeof sig_dom); 172 _Static_assert(sizeof msg == 65, "signed message is 65 bytes"); 173 if (!MUT(12) && aienos_`ed25519`_verify(r + OSC_RCPT_SIGNED_BYTES, msg, sizeof msg, pk) != AIENOS_SIG_OK) 174 return OSC_RCPT_BAD_SIGNATURE; 175 } 176 177 /* 4. Revocation: receipt id, then signer key fingerprint */ 178 if (!MUT(13) && in_list(info->id, p->rcpt_revoked_ids, p->n_rcpt_revoked_ids)) { 179 info->revoked = OSC_RCPT_REVOKED_ID; 180 return OSC_RCPT_REVOKED; 181 } 182 if (!MUT(14) && in_list(fp, p->rcpt_revoked_keys, p->n_rcpt_revoked_keys)) { 183 info->revoked = OSC_RCPT_REVOKED_KEY; 184 return OSC_RCPT_REVOKED; 185 } 186 187 /* 5. Freshness in policy epochs (no clock), window inclusive at both ends */ 188 if (!MUT(15) && info->policy_epoch < p->rcpt_epoch_floor) 189 return OSC_RCPT_STALE_EPOCH; 190 if (!MUT(16) && p->rcpt_epoch_now > info->expiry_epoch) 191 return OSC_RCPT_EXPIRED; 192 if (!MUT(17) && p->rcpt_epoch_now < info->policy_epoch) 193 return OSC_RCPT_NOT_YET_VALID; 194 195 /* 6. Checker identity: in the accepted list (an empty list refuses every receipt) */ 196 if (!MUT(18) && !in_list(r + 144, p->rcpt_checkers, p->n_rcpt_checkers)) 197 return OSC_RCPT_UNTRUSTED_CHECKER; 198 199 /* 7. Binding to the unit being launched */ 200 if (!MUT(19) && (!p->rcpt_target || !eq32(r + 208, p->rcpt_target))) 201 return OSC_RCPT_WRONG_TARGET; 202 if (!MUT(20) && !eq32(r + 240, p->rcpt_firmware ? p->rcpt_firmware : zero32)) 203 return OSC_RCPT_WRONG_FIRMWARE; 204 if (!MUT(21) && !eq32(r + 48, a->ir_sha256)) 205 return OSC_RCPT_PROGRAM_MISMATCH; 206 if (!MUT(22) && !eq32(r + 80, a->code_sha256)) 207 return OSC_RCPT_EXECUTABLE_MISMATCH; 208 if (!MUT(23) && !eq32(r + 176, p->rcpt_producer ? p->rcpt_producer : zero32)) 209 return OSC_RCPT_WRONG_PRODUCER; 210 { 211 uint8_t deps[32]; 212 empty_deps(deps); 213 if (!MUT(24) && !eq32(r + 112, deps)) 214 return OSC_RCPT_DEPENDENCIES_MISMATCH; 215 } 216 217 /* 8. Evidence class 2 (spec section 10): valid only when the same LRAT proofs are given and replayed. The 218 * kernel is given none and has no LRAT checker, so class 2 is refused (fail closed). The spec does not place 219 * this check in the section 6 order; it runs last, after every section 6 step passed. */ 220 if (!MUT(25) && ev == 2) 221 return OSC_RCPT_LRAT_PROOFS_REQUIRED; 222 return 0; 223 }