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/src/checker2/c2_receipt.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 / src / checker2 / c2_receipt.c` 308 lines ยท 13595 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 /* c2_receipt.c: signed Omega realization receipts v1. Normative layout: spec/one-layer-receipts.md. 18 * 19 * Signature code: the vendored in-house Ed25519 (src/searchtrace/sig, aienos native/sig @ `913b962`, 20 * PROVENANCE.txt there). SHA-256: src/sha256.c. No other dependency. */ 21 #include "c2_receipt.h" 22 23 #include 24 25 #include "sha256.h" 26 #include "searchtrace/sig/aienos_sig.h" 27 28 static const uint8_t MAGIC[8] = {'O', 'M', 'R', 'C', 'P', 'T', '1', 0}; 29 30 /* `578ada3c`...9e76: aienos-owner-ceremony/20261014T114310Z/slot-9c-public.pem and 31 * owner-pubkeys.txt (aienos#380). Public data; tests/checker2/fixtures/owner holds the ceremony 32 * statement and its real signature, which test_c2_receipt verifies against this key. */ 33 const uint8_t c2r_owner_root_pk[32] = { 34 0x57, 0x8a, 0xda, 0x3c, 0xb0, 0xc2, 0xd1, 0x54, 0x62, 0x3a, 0xc0, 0x89, 0x19, 0x10, 0x23, 0xc8, 35 0xad, 0x4b, 0xc5, 0xe6, 0x3b, 0x8c, 0xcd, 0x1a, 0x38, 0xdb, 0x2d, 0x67, 0x9b, 0x8c, 0x9e, 0x76}; 36 37 /* Derived from c2r_test_seed by aienos_`ed25519`_public_key; test_c2_receipt re-derives and compares. */ 38 const uint8_t c2r_test_pk[32] = { 39 0x60, 0x28, 0x10, 0xd4, 0x3c, 0x01, 0x15, 0xdf, 0x87, 0x7b, 0x0c, 0xf8, 0xc3, 0x27, 0x01, 0x0e, 40 0x5a, 0x7e, 0x57, 0x04, 0x90, 0xc8, 0x5e, 0x31, 0x46, 0x6c, 0x0c, 0xff, 0x6c, 0xa3, 0x74, 0xc5}; 41 42 void c2r_test_seed(uint8_t seed[32]) 43 { 44 static const char s[] = "OMEGA-CHECKER2-TEST-KEY-v1 NOT FOR PRODUCTION"; 45 sha256_hash((const uint8_t *)s, sizeof s - 1, seed); 46 } 47 48 const char *c2r_reason(int code) 49 { 50 switch (code) { 51 case C2R_OK: return "OK"; 52 case C2R_E_SIZE: return "SIZE"; 53 case C2R_E_MAGIC: return "MAGIC"; 54 case C2R_E_VERSION: return "VERSION"; 55 case C2R_E_ALG: return "SIGNATURE_ALGORITHM"; 56 case C2R_E_CLASS: return "SIGNER_CLASS"; 57 case C2R_E_EVIDENCE: return "EVIDENCE_CLASS"; 58 case C2R_E_EPOCH_ORDER: return "EPOCH_ORDER"; 59 case C2R_E_TEST_IN_PRODUCTION: return "TEST_SIGNER_IN_PRODUCTION"; 60 case C2R_E_UNTRUSTED_SIGNER: return "UNTRUSTED_SIGNER"; 61 case C2R_E_BAD_SIGNATURE: return "BAD_SIGNATURE"; 62 case C2R_E_REVOKED: return "REVOKED"; 63 case C2R_E_STALE: return "STALE_EPOCH"; 64 case C2R_E_EXPIRED: return "EXPIRED"; 65 case C2R_E_NOT_YET_VALID: return "NOT_YET_VALID"; 66 case C2R_E_WRONG_TARGET: return "WRONG_TARGET"; 67 case C2R_E_WRONG_FIRMWARE: return "WRONG_FIRMWARE"; 68 case C2R_E_PROGRAM_MISMATCH: return "PROGRAM_MISMATCH"; 69 case C2R_E_EXECUTABLE_MISMATCH: return "EXECUTABLE_MISMATCH"; 70 case C2R_E_ARG: return "ARGUMENT"; 71 case C2R_E_KEY_CLASS: return "KEY_CLASS"; 72 case C2R_E_UNTRUSTED_CHECKER: return "UNTRUSTED_CHECKER"; 73 case C2R_E_WRONG_PRODUCER: return "WRONG_PRODUCER"; 74 case C2R_E_DEPS_MISMATCH: return "DEPENDENCIES_MISMATCH"; 75 default: return "UNKNOWN"; 76 } 77 } 78 79 static void put16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } 80 static void put64(uint8_t *p, uint64_t v) 81 { 82 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); 83 } 84 static uint16_t get16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } 85 static uint64_t get64(const uint8_t *p) 86 { 87 uint64_t v = 0; 88 for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; 89 return v; 90 } 91 92 void c2r_target_id(const char *name, uint8_t out[32]) 93 { 94 static const char tag[] = "OMEGA-TARGET-V1"; /* sizeof includes the NUL */ 95 sha256_ctx c; 96 sha256_init(&c); 97 sha256_update(&c, (const uint8_t *)tag, sizeof tag); 98 sha256_update(&c, (const uint8_t *)name, strlen(name)); 99 sha256_final(&c, out); 100 } 101 102 int c2r_deps_digest(const uint8_t (*ids)[32], unsigned n, uint8_t out[32]) 103 { 104 static const char tag[] = "OMEGA-RECEIPT-DEPS-V1"; 105 for (unsigned i = 1; i < n; i++) 106 if (memcmp(ids[i - 1], ids[i], 32) >= 0) return -1; 107 uint8_t cnt[4] = {(uint8_t)n, (uint8_t)(n >> 8), (uint8_t)(n >> 16), (uint8_t)(n >> 24)}; 108 sha256_ctx c; 109 sha256_init(&c); 110 sha256_update(&c, (const uint8_t *)tag, sizeof tag); 111 sha256_update(&c, cnt, 4); 112 for (unsigned i = 0; i < n; i++) sha256_update(&c, ids[i], 32); 113 sha256_final(&c, out); 114 return 0; 115 } 116 117 /* Offsets (spec/one-layer-receipts.md section 2). */ 118 enum { 119 O_MAGIC = 0, O_VER = 8, O_CLASS = 10, O_ALG = 12, O_EV = 14, 120 O_REQ = 16, O_PROG = 48, O_EXE = 80, O_DEPS = 112, O_CHK = 144, O_PCHK = 176, 121 O_TGT = 208, O_FW = 240, O_IN = 272, O_EPOCH = 304, O_EXP = 312, O_KFP = 320, O_SIG = 352 122 }; 123 124 void c2r_encode(const C2Receipt *r, uint8_t out[C2R_SIZE]) 125 { 126 memcpy(out + O_MAGIC, MAGIC, 8); 127 put16(out + O_VER, r->version); 128 put16(out + O_CLASS, r->signer_class); 129 put16(out + O_ALG, r->alg); 130 put16(out + O_EV, r->evidence_class); 131 memcpy(out + O_REQ, r->requirements, 32); 132 memcpy(out + O_PROG, r->program, 32); 133 memcpy(out + O_EXE, r->executable, 32); 134 memcpy(out + O_DEPS, r->dependencies, 32); 135 memcpy(out + O_CHK, r->checker, 32); 136 memcpy(out + O_PCHK, r->producer_checker, 32); 137 memcpy(out + O_TGT, r->target, 32); 138 memcpy(out + O_FW, r->firmware, 32); 139 memcpy(out + O_IN, r->input, 32); 140 put64(out + O_EPOCH, r->policy_epoch); 141 put64(out + O_EXP, r->expiry_epoch); 142 memcpy(out + O_KFP, r->key_fp, 32); 143 memcpy(out + O_SIG, r->sig, 64); 144 } 145 146 int c2r_decode(const uint8_t *b, size_t len, C2Receipt *r) 147 { 148 if (!b || !r) return C2R_E_ARG; 149 if (len != C2R_SIZE) return C2R_E_SIZE; 150 if (memcmp(b, MAGIC, 8) != 0) return C2R_E_MAGIC; /* GUARD:magic */ 151 r->version = get16(b + O_VER); 152 r->signer_class = get16(b + O_CLASS); 153 r->alg = get16(b + O_ALG); 154 r->evidence_class = get16(b + O_EV); 155 if (r->version != C2R_FORMAT_VERSION) return C2R_E_VERSION; 156 if (r->alg != C2R_ALG_ED25519) return C2R_E_ALG; 157 if (r->signer_class != C2R_CLASS_TEST && r->signer_class != C2R_CLASS_OWNER) return C2R_E_CLASS; 158 if (r->evidence_class != C2R_EV_IR_CHECKED && r->evidence_class != C2R_EV_IR_CHECKED_LRAT) 159 return C2R_E_EVIDENCE; 160 memcpy(r->requirements, b + O_REQ, 32); 161 memcpy(r->program, b + O_PROG, 32); 162 memcpy(r->executable, b + O_EXE, 32); 163 memcpy(r->dependencies, b + O_DEPS, 32); 164 memcpy(r->checker, b + O_CHK, 32); 165 memcpy(r->producer_checker, b + O_PCHK, 32); 166 memcpy(r->target, b + O_TGT, 32); 167 memcpy(r->firmware, b + O_FW, 32); 168 memcpy(r->input, b + O_IN, 32); 169 r->policy_epoch = get64(b + O_EPOCH); 170 r->expiry_epoch = get64(b + O_EXP); 171 if (r->expiry_epoch < r->policy_epoch) return C2R_E_EPOCH_ORDER; /* GUARD:epoch-order */ 172 memcpy(r->key_fp, b + O_KFP, 32); 173 memcpy(r->sig, b + O_SIG, 64); 174 return C2R_OK; 175 } 176 177 void c2r_id(const uint8_t buf[C2R_SIZE], uint8_t out[32]) { sha256_hash(buf, C2R_SIZE, out); } 178 179 void c2r_signed_message(const uint8_t buf[C2R_SIZE], uint8_t msg[C2R_SIGNED_MSG_LEN]) 180 { 181 static const char body_tag[] = "OMEGA-REALIZATION-RECEIPT-V1"; 182 static const char sig_tag[] = "OMEGA-REALIZATION-RECEIPT-SIG-V1"; 183 _Static_assert(sizeof sig_tag + 32 == C2R_SIGNED_MSG_LEN, "signed message length"); 184 sha256_ctx c; 185 sha256_init(&c); 186 sha256_update(&c, (const uint8_t *)body_tag, sizeof body_tag); 187 sha256_update(&c, buf, C2R_SIGNED_LEN); 188 memcpy(msg, sig_tag, sizeof sig_tag); 189 sha256_final(&c, msg + sizeof sig_tag); 190 } 191 192 static int fields_ok(const C2Receipt *r) 193 { 194 uint8_t tmp[C2R_SIZE]; 195 C2Receipt chk; 196 C2Receipt copy = *r; 197 memset(copy.key_fp, 0, 32); 198 memset(copy.sig, 0, 64); 199 c2r_encode(©, tmp); 200 return c2r_decode(tmp, C2R_SIZE, &chk); 201 } 202 203 int c2r_sign_test(C2Receipt *r, const uint8_t seed[32], uint8_t out[C2R_SIZE]) 204 { 205 if (!r || !seed || !out) return C2R_E_ARG; 206 if (r->signer_class != C2R_CLASS_TEST) return C2R_E_KEY_CLASS; /* GUARD:sign-test-class-only */ 207 uint8_t pk[32]; 208 if (aienos_`ed25519`_public_key(pk, seed) != AIENOS_SIG_OK) return C2R_E_ARG; 209 /* Only the TEST key may sign TEST class here; any other secret is refused, so a stray key can 210 * never mint a receipt that a TEST-anchored verifier would also take. */ 211 if (memcmp(pk, c2r_test_pk, 32) != 0) return C2R_E_KEY_CLASS; /* GUARD:sign-test-key-only */ 212 int e = fields_ok(r); 213 if (e) return e; 214 sha256_hash(pk, 32, r->key_fp); 215 memset(r->sig, 0, 64); 216 c2r_encode(r, out); 217 uint8_t msg[C2R_SIGNED_MSG_LEN]; 218 c2r_signed_message(out, msg); 219 if (aienos_`ed25519`_sign(r->sig, msg, sizeof msg, seed) != AIENOS_SIG_OK) return C2R_E_ARG; 220 memcpy(out + O_SIG, r->sig, 64); 221 return C2R_OK; 222 } 223 224 int c2r_attach(C2Receipt *r, const uint8_t pk[32], const uint8_t sig[64], uint8_t out[C2R_SIZE]) 225 { 226 if (!r || !pk || !sig || !out) return C2R_E_ARG; 227 int e = fields_ok(r); 228 if (e) return e; 229 sha256_hash(pk, 32, r->key_fp); 230 memcpy(r->sig, sig, 64); 231 c2r_encode(r, out); 232 uint8_t msg[C2R_SIGNED_MSG_LEN]; 233 c2r_signed_message(out, msg); 234 if (aienos_`ed25519`_verify(sig, msg, sizeof msg, pk) != AIENOS_SIG_OK) return C2R_E_BAD_SIGNATURE; 235 return C2R_OK; 236 } 237 238 /* The anchor of the receipt's class whose fingerprint matches, or NULL. */ 239 static const uint8_t *anchor_for(const uint8_t (*set)[32], unsigned n, const uint8_t fp[32]) 240 { 241 for (unsigned i = 0; set && i < n; i++) { 242 uint8_t h[32]; 243 sha256_hash(set[i], 32, h); 244 if (memcmp(h, fp, 32) == 0) return set[i]; 245 } 246 return NULL; 247 } 248 249 static int in_set(const uint8_t (*set)[32], unsigned n, const uint8_t v[32]) 250 { 251 for (unsigned i = 0; set && i < n; i++) 252 if (memcmp(set[i], v, 32) == 0) return 1; 253 return 0; 254 } 255 256 int c2r_verify(const uint8_t *buf, size_t len, const C2ReceiptPolicy *pol, const C2ReceiptExpect *ex, 257 C2Receipt *out) 258 { 259 C2Receipt r; 260 if (!pol) return C2R_E_ARG; 261 /* 1. structure */ 262 int e = c2r_decode(buf, len, &r); 263 if (e) return e; 264 /* 2. class policy: production takes OWNER only, and only Drake's root */ 265 if (pol->production && r.signer_class != C2R_CLASS_OWNER) return C2R_E_TEST_IN_PRODUCTION; /* GUARD:production-class */ 266 const uint8_t *key; 267 if (r.signer_class == C2R_CLASS_TEST) { 268 key = anchor_for(pol->test_anchors, pol->n_test, r.key_fp); 269 } else { 270 key = anchor_for(pol->owner_anchors, pol->n_owner, r.key_fp); 271 /* production: an owner anchor that is not the compiled-in root is not an owner */ 272 if (key && pol->production && memcmp(key, c2r_owner_root_pk, 32) != 0) key = NULL; /* GUARD:production-root */ 273 } 274 if (!key) return C2R_E_UNTRUSTED_SIGNER; /* GUARD:anchor */ 275 /* a TEST-class receipt signed by an owner anchor key (or the reverse) is refused: the key 276 * must not be an anchor of the other class either */ 277 int other = r.signer_class == C2R_CLASS_TEST ? anchor_for(pol->owner_anchors, pol->n_owner, r.key_fp) != NULL 278 : anchor_for(pol->test_anchors, pol->n_test, r.key_fp) != NULL; 279 if (other) return C2R_E_UNTRUSTED_SIGNER; /* GUARD:cross-class */ 280 /* 3. signature over the domain-separated digest */ 281 uint8_t msg[C2R_SIGNED_MSG_LEN]; 282 c2r_signed_message(buf, msg); 283 if (aienos_`ed25519`_verify(r.sig, msg, sizeof msg, key) != AIENOS_SIG_OK) return C2R_E_BAD_SIGNATURE; /* GUARD:signature */ 284 /* 4. revocation (receipt id, then signer key) */ 285 uint8_t id[32]; 286 c2r_id(buf, id); 287 if (in_set(pol->revoked_ids, pol->n_revoked_ids, id)) return C2R_E_REVOKED; /* GUARD:revoked-id */ 288 if (in_set(pol->revoked_keys, pol->n_revoked_keys, r.key_fp)) return C2R_E_REVOKED; /* GUARD:revoked-key */ 289 /* 5. freshness */ 290 if (r.policy_epoch < pol->epoch_floor) return C2R_E_STALE; /* GUARD:stale */ 291 if (pol->current_epoch < r.policy_epoch) return C2R_E_NOT_YET_VALID; /* GUARD:not-yet-valid */ 292 if (pol->current_epoch > r.expiry_epoch) return C2R_E_EXPIRED; /* GUARD:expired */ 293 /* 6. checker identity: only verdicts of a checker2 build the verifier anchors count; an empty 294 * or missing list refuses everything */ 295 if (!in_set(pol->checkers, pol->n_checkers, r.checker)) return C2R_E_UNTRUSTED_CHECKER; /* GUARD:checker-identity */ 296 /* 7. binding to what is being admitted */ 297 if (ex) { 298 if (ex->target && memcmp(ex->target, r.target, 32) != 0) return C2R_E_WRONG_TARGET; /* GUARD:target */ 299 if (ex->firmware && memcmp(ex->firmware, r.firmware, 32) != 0) return C2R_E_WRONG_FIRMWARE; /* GUARD:firmware */ 300 if (ex->program && memcmp(ex->program, r.program, 32) != 0) return C2R_E_PROGRAM_MISMATCH; /* GUARD:program */ 301 if (ex->executable && memcmp(ex->executable, r.executable, 32) != 0) return C2R_E_EXECUTABLE_MISMATCH; /* GUARD:executable */ 302 if (ex->producer_checker && memcmp(ex->producer_checker, r.producer_checker, 32) != 0) return C2R_E_WRONG_PRODUCER; /* GUARD:producer */ 303 /* the dependency ids are bound by digest only; v1 does not resolve dependency receipts */ 304 if (ex->dependencies && memcmp(ex->dependencies, r.dependencies, 32) != 0) return C2R_E_DEPS_MISMATCH; /* GUARD:dependencies */ 305 } 306 if (out) *out = r; 307 return C2R_OK; 308 }