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 / receipt.c
| 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 | /* receipt.c -- SEED-0B admission receipts (port of aienos-artifact |
| 18 | * receipt.rs): 512-byte canonical record, digest, nonce, validation and the |
| 19 | * TEST-ONLY qualification signature. */ |
| 20 | #include "ck_artifact.h" |
| 21 | #include "aienos_sig.h" |
| 22 | #include "sha256.h" |
| 23 | |
| 24 | static const char rmagic[8] = "AIENRCP"; |
| 25 | static const char sig_dom[] = "AIENOS-ADMISSION-RECEIPT-SIGNATURE-V1"; |
| 26 | |
| 27 | const char *cka_status_name(uint32_t s) |
| 28 | { |
| 29 | static const char *const n[6] = {"NotRun", "Exited", "Timeout", |
| 30 | "Fault", "BadSyscall", "ResourceOverrun"}; |
| 31 | return s < 6 ? n[s] : "?"; |
| 32 | } |
| 33 | |
| 34 | void cka_receipt_nonce(const uint8_t verifier[32], uint64_t seq, uint8_t out[16]) |
| 35 | { |
| 36 | static const char dom[] = "AIENOS-ADMISSION-RECEIPT-NONCE-V1"; |
| 37 | uint8_t s[8], d[32]; |
| 38 | sha256_ctx c; |
| 39 | cka_wr64(s, seq); |
| 40 | sha256_init(&c); |
| 41 | sha256_update(&c, (const uint8_t *)dom, sizeof dom); |
| 42 | sha256_update(&c, verifier, 32); |
| 43 | sha256_update(&c, s, 8); |
| 44 | sha256_final(&c, d); |
| 45 | for (int i = 0; i < 16; i++) |
| 46 | out[i] = d[i]; |
| 47 | } |
| 48 | |
| 49 | static void put(uint8_t *o, const uint8_t *s, unsigned n) |
| 50 | { |
| 51 | for (unsigned i = 0; i < n; i++) |
| 52 | o[i] = s[i]; |
| 53 | } |
| 54 | |
| 55 | void cka_receipt_encode(const struct cka_receipt *r, uint8_t o[CKA_RECEIPT_SIZE]) |
| 56 | { |
| 57 | for (unsigned i = 0; i < CKA_RECEIPT_SIZE; i++) |
| 58 | o[i] = 0; |
| 59 | put(o, (const uint8_t *)rmagic, 8); |
| 60 | cka_wr16(o + 10, 96); |
| 61 | cka_wr32(o + 12, CKA_RECEIPT_SIZE); |
| 62 | cka_wr32(o + 16, r->flags); |
| 63 | cka_wr16(o + 20, r->decision); |
| 64 | cka_wr16(o + 22, r->tier); |
| 65 | cka_wr64(o + 24, r->seq); |
| 66 | put(o + 32, r->nonce, 16); |
| 67 | cka_wr64(o + 48, r->time); |
| 68 | cka_wr32(o + 64, r->status); |
| 69 | cka_wr32(o + 68, (uint32_t)r->exit); |
| 70 | cka_wr32(o + 72, r->result_flags); |
| 71 | cka_wr16(o + 76, r->stage); |
| 72 | cka_wr16(o + 78, r->reason); |
| 73 | cka_wr32(o + 80, r->syscalls); |
| 74 | cka_wr32(o + 84, r->reads); |
| 75 | cka_wr32(o + 88, r->denials); |
| 76 | cka_wr32(o + 92, r->frames); |
| 77 | put(o + 96, r->id, 32); |
| 78 | put(o + 128, r->payload, 32); |
| 79 | put(o + 160, r->signer, 32); |
| 80 | put(o + 192, r->policy, 32); |
| 81 | put(o + 224, r->requested, 32); |
| 82 | put(o + 256, r->granted, 32); |
| 83 | put(o + 288, r->resources, 32); |
| 84 | put(o + 320, r->verifier, 32); |
| 85 | put(o + 352, r->machine, 32); |
| 86 | cka_wr64(o + 384, r->generation); |
| 87 | cka_wr64(o + 392, r->context); |
| 88 | if (r->sig_alg) { |
| 89 | cka_wr16(o + 400, r->sig_alg); |
| 90 | put(o + 404, r->fp, 32); |
| 91 | put(o + 436, r->sig, 64); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static int nz(const uint8_t *b, unsigned from, unsigned to) |
| 96 | { |
| 97 | uint8_t acc = 0; |
| 98 | for (unsigned i = from; i < to; i++) |
| 99 | acc |= b[i]; |
| 100 | return acc != 0; |
| 101 | } |
| 102 | |
| 103 | int cka_receipt_decode(const uint8_t b[CKA_RECEIPT_SIZE], struct cka_receipt *r) |
| 104 | { |
| 105 | for (int i = 0; i < 8; i++) |
| 106 | if (b[i] != (uint8_t)rmagic[i]) |
| 107 | return CKA_BAD_MAGIC; |
| 108 | if (cka_rd16(b + 8) != 0 || cka_rd16(b + 10) != 96 || cka_rd32(b + 12) != CKA_RECEIPT_SIZE) |
| 109 | return CKA_UNSUPPORTED_VERSION; |
| 110 | r->flags = cka_rd32(b + 16); |
| 111 | r->result_flags = cka_rd32(b + 72); |
| 112 | if ((r->flags & ~7u) || (r->result_flags & ~0xffu)) |
| 113 | return CKA_UNSUPPORTED_FLAGS; |
| 114 | if (nz(b, 56, 64) || nz(b, 500, 512)) |
| 115 | return CKA_RESERVED_NONZERO; |
| 116 | r->sig_alg = 0; |
| 117 | if (nz(b, 400, 500)) { |
| 118 | if (cka_rd16(b + 400) != 1) |
| 119 | return CKA_BAD_SIG_FORMAT; |
| 120 | if (b[402] || b[403]) |
| 121 | return CKA_RESERVED_NONZERO; |
| 122 | r->sig_alg = 1; |
| 123 | } |
| 124 | r->decision = cka_rd16(b + 20); |
| 125 | r->tier = cka_rd16(b + 22); |
| 126 | r->status = cka_rd32(b + 64); |
| 127 | if ((r->decision != CKR_ADMITTED && r->decision != CKR_REJECTED) || |
| 128 | !CKA_TIER_OK(r->tier) || r->status > CKR_RESOURCE_OVERRUN) |
| 129 | return CKA_MALFORMED_RECEIPT; |
| 130 | r->seq = cka_rd64(b + 24); |
| 131 | put(r->nonce, b + 32, 16); |
| 132 | r->time = cka_rd64(b + 48); |
| 133 | r->exit = (int32_t)cka_rd32(b + 68); |
| 134 | r->stage = cka_rd16(b + 76); |
| 135 | r->reason = cka_rd16(b + 78); |
| 136 | r->syscalls = cka_rd32(b + 80); |
| 137 | r->reads = cka_rd32(b + 84); |
| 138 | r->denials = cka_rd32(b + 88); |
| 139 | r->frames = cka_rd32(b + 92); |
| 140 | put(r->id, b + 96, 32); |
| 141 | put(r->payload, b + 128, 32); |
| 142 | put(r->signer, b + 160, 32); |
| 143 | put(r->policy, b + 192, 32); |
| 144 | put(r->requested, b + 224, 32); |
| 145 | put(r->granted, b + 256, 32); |
| 146 | put(r->resources, b + 288, 32); |
| 147 | put(r->verifier, b + 320, 32); |
| 148 | put(r->machine, b + 352, 32); |
| 149 | r->generation = cka_rd64(b + 384); |
| 150 | r->context = cka_rd64(b + 392); |
| 151 | put(r->fp, b + 404, 32); |
| 152 | put(r->sig, b + 436, 64); |
| 153 | return cka_receipt_validate(r); /* as receipt::decode in Rust */ |
| 154 | } |
| 155 | |
| 156 | int cka_receipt_validate(const struct cka_receipt *r) |
| 157 | { |
| 158 | static const uint8_t zero[32]; |
| 159 | int bad = CKA_MALFORMED_RECEIPT; |
| 160 | uint8_t acc = 0; |
| 161 | for (int i = 0; i < 32; i++) |
| 162 | acc |= (uint8_t)(r->machine[i] ^ zero[i]); |
| 163 | if (!(r->flags & 1) && acc) |
| 164 | return bad; |
| 165 | if (!(r->flags & 2) && (r->generation || r->context)) |
| 166 | return bad; |
| 167 | if (!(r->flags & 4) && r->time) |
| 168 | return bad; |
| 169 | int reason_ok = (r->reason >= 1 && r->reason <= 19) || (r->reason >= 0x101 && r->reason <= 0x10a); |
| 170 | if (r->stage > 9 || (r->reason && !reason_ok)) |
| 171 | return bad; |
| 172 | if (r->decision == CKR_REJECTED) { |
| 173 | if (!r->stage || !r->reason || r->status != CKR_NOT_RUN || r->exit || r->syscalls || |
| 174 | r->reads || r->denials || (r->result_flags & ~CKR_F_RECLAIMED)) |
| 175 | return bad; |
| 176 | } else if (r->stage || r->reason) { |
| 177 | return bad; |
| 178 | } |
| 179 | if ((r->result_flags & CKR_F_CANARY) && (r->status != CKR_EXITED || r->exit)) |
| 180 | return bad; |
| 181 | uint8_t n[16]; |
| 182 | cka_receipt_nonce(r->verifier, r->seq, n); |
| 183 | acc = 0; |
| 184 | for (int i = 0; i < 16; i++) |
| 185 | acc |= (uint8_t)(n[i] ^ r->nonce[i]); |
| 186 | return acc ? bad : CKA_OK; |
| 187 | } |
| 188 | |
| 189 | void cka_receipt_digest(const uint8_t b[CKA_RECEIPT_SIZE], uint8_t out[32]) |
| 190 | { |
| 191 | static const char dom[] = "AIENOS-ADMISSION-RECEIPT-V1"; |
| 192 | cka_digest_domain(dom, sizeof dom, b, CKA_RECEIPT_SIGNED_LEN, out); |
| 193 | } |
| 194 | |
| 195 | int cka_receipt_is_signed(const uint8_t b[CKA_RECEIPT_SIZE]) |
| 196 | { |
| 197 | return nz(b, CKA_RECEIPT_SIGNED_LEN, CKA_RECEIPT_SIZE); |
| 198 | } |
| 199 | |
| 200 | static void sig_message(const uint8_t digest[32], uint8_t *m, size_t *len) |
| 201 | { |
| 202 | for (size_t i = 0; i < sizeof sig_dom; i++) |
| 203 | m[i] = (uint8_t)sig_dom[i]; |
| 204 | put(m + sizeof sig_dom, digest, 32); |
| 205 | *len = sizeof sig_dom + 32; |
| 206 | } |
| 207 | |
| 208 | int cka_receipt_sign(uint8_t b[CKA_RECEIPT_SIZE], const uint8_t seed[32]) |
| 209 | { |
| 210 | struct cka_receipt r; |
| 211 | if (cka_receipt_is_signed(b)) |
| 212 | return CKA_BAD_SIG_FORMAT; |
| 213 | int rc = cka_receipt_decode(b, &r); |
| 214 | if (rc) |
| 215 | return rc; |
| 216 | uint8_t d[32], pk[32], m[96]; |
| 217 | size_t mlen; |
| 218 | cka_receipt_digest(b, d); |
| 219 | sig_message(d, m, &mlen); |
| 220 | if (aienos_ed25519_public_key(pk, seed) != AIENOS_SIG_OK || |
| 221 | aienos_ed25519_sign(b + 436, m, mlen, seed) != AIENOS_SIG_OK) |
| 222 | return CKA_BAD_SIGNATURE; |
| 223 | cka_wr16(b + 400, 1); |
| 224 | sha256_hash(pk, 32, b + 404); |
| 225 | return CKA_OK; |
| 226 | } |
| 227 | |
| 228 | int cka_receipt_verify(const uint8_t b[CKA_RECEIPT_SIZE], const uint8_t (*anchors)[32], |
| 229 | unsigned nanchors) |
| 230 | { |
| 231 | struct cka_receipt r; |
| 232 | int rc = cka_receipt_decode(b, &r); |
| 233 | if (rc) |
| 234 | return rc; |
| 235 | if (!r.sig_alg) |
| 236 | return CKA_BAD_SIG_FORMAT; |
| 237 | const uint8_t *key = 0; |
| 238 | uint8_t fp[32]; |
| 239 | for (unsigned i = 0; i < nanchors && !key; i++) { |
| 240 | sha256_hash(anchors[i], 32, fp); |
| 241 | uint8_t acc = 0; |
| 242 | for (int k = 0; k < 32; k++) |
| 243 | acc |= (uint8_t)(fp[k] ^ r.fp[k]); |
| 244 | if (!acc) |
| 245 | key = anchors[i]; |
| 246 | } |
| 247 | if (!key) |
| 248 | return CKA_UNTRUSTED_SIGNER; |
| 249 | uint8_t d[32], m[96]; |
| 250 | size_t mlen; |
| 251 | cka_receipt_digest(b, d); |
| 252 | sig_message(d, m, &mlen); |
| 253 | return aienos_ed25519_verify(r.sig, m, mlen, key) == AIENOS_SIG_OK ? CKA_OK : CKA_BAD_SIGNATURE; |
| 254 | } |
| 255 | |
| 256 | void cka_verifier_identity(const char *commit, uint8_t feature, uint8_t out[32]) |
| 257 | { |
| 258 | static const char dom[] = "AIENOS-VERIFIER-IDENTITY-V1"; |
| 259 | uint8_t id[45]; |
| 260 | int ok = 1; |
| 261 | for (int i = 0; i < 40 && ok; i++) { |
| 262 | char c = commit[i]; |
| 263 | ok = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); |
| 264 | id[i] = (uint8_t)c; |
| 265 | } |
| 266 | if (ok && commit[40] == 0) { |
| 267 | cka_wr16(id + 40, CKA_TARGET_AARCH64_LE); |
| 268 | cka_wr16(id + 42, CKA_ABI_V1); |
| 269 | id[44] = feature; |
| 270 | cka_digest_domain(dom, sizeof dom, id, sizeof id, out); |
| 271 | } else { |
| 272 | static const char unpinned[] = "unpinned-build"; |
| 273 | cka_digest_domain(dom, sizeof dom, (const uint8_t *)unpinned, sizeof unpinned - 1, out); |
| 274 | } |
| 275 | } |