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 / svc / continuity_resolve_sealed.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 | /* continuity_resolve_sealed.c -- see continuity_resolve_sealed.h. */ |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "continuity_resolve_sealed.h" |
| 21 | |
| 22 | static uint32_t sealed_count(void *ctx) |
| 23 | { |
| 24 | const ss_store *s = ctx; |
| 25 | return s->nclaims; |
| 26 | } |
| 27 | |
| 28 | static int sealed_entry(void *ctx, uint32_t i, uint16_t *kind, uint16_t *version) |
| 29 | { |
| 30 | const ss_store *s = ctx; |
| 31 | if (i >= s->nclaims) return ST_E_INVALID_OBJECT; |
| 32 | *kind = s->ws->claims[i].c.obj.object_kind; |
| 33 | *version = s->ws->claims[i].c.obj.object_version; |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | static int sealed_read(void *ctx, uint32_t i, uint8_t *out, size_t cap, size_t *len) |
| 38 | { |
| 39 | ss_store *s = ctx; |
| 40 | if (i >= s->nclaims) return ST_E_INVALID_OBJECT; |
| 41 | uint8_t sid[32]; |
| 42 | memcpy(sid, s->ws->claims[i].sid, 32); |
| 43 | uint16_t kind = 0; |
| 44 | int rc = ss_read(s, sid, out, cap, len, &kind); |
| 45 | if (rc != 0) return rc; |
| 46 | /* The claim and the envelope binding must agree on the application kind. */ |
| 47 | if (kind != s->ws->claims[i].c.obj.object_kind) return SS_E_ENVELOPE; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static uint64_t sealed_generation(void *ctx) |
| 52 | { |
| 53 | return ss_generation(ctx); |
| 54 | } |
| 55 | |
| 56 | static int sealed_mount_state(void *ctx) |
| 57 | { |
| 58 | const ss_store *s = ctx; |
| 59 | return s->st.state == ST_VALID ? CR_MOUNT_VALID : CR_MOUNT_DEGRADED; |
| 60 | } |
| 61 | |
| 62 | void cr_bind_sealed(struct cr_source *out, ss_store *s) |
| 63 | { |
| 64 | out->ctx = s; |
| 65 | out->count = sealed_count; |
| 66 | out->entry = sealed_entry; |
| 67 | out->read = sealed_read; |
| 68 | out->mount_state = sealed_mount_state; |
| 69 | out->generation = sealed_generation; |
| 70 | } |
| 71 | |
| 72 | void cr_state_digest_dev(const st_dev *dev, uint8_t out[32]) |
| 73 | { |
| 74 | uint8_t raw[2][SV1_UNIT]; |
| 75 | sha256_ctx h; |
| 76 | for (unsigned u = 0; u < 2; u++) { |
| 77 | memset(raw[u], 0, SV1_UNIT); |
| 78 | if (!dev || !dev->read_unit || dev->read_unit(dev->ctx, u, raw[u]) != 0) |
| 79 | memset(raw[u], 0, SV1_UNIT); /* recovery_core.rs:109-133: error leaves zero */ |
| 80 | } |
| 81 | sha256_init(&h); |
| 82 | sha256_update(&h, raw[0], SV1_UNIT); |
| 83 | sha256_update(&h, raw[1], SV1_UNIT); |
| 84 | sha256_final(&h, out); |
| 85 | } |
| 86 | |
| 87 | static int sealed_transact(void *ctx, const struct cr_wobj *objs, size_t n) |
| 88 | { |
| 89 | struct cr_sealed_sink *k = ctx; |
| 90 | ss_object so[SS_MAX_OBJECTS]; |
| 91 | if (n == 0 || n > SS_MAX_OBJECTS) return SS_E_ARG; |
| 92 | for (size_t i = 0; i < n; i++) { |
| 93 | so[i].kind = objs[i].kind; |
| 94 | so[i].version = objs[i].version; |
| 95 | so[i].bytes = objs[i].bytes; |
| 96 | so[i].len = objs[i].len; |
| 97 | } |
| 98 | return ss_transact(k->s, so, n, k->hook, k->hook_arg, NULL); |
| 99 | } |
| 100 | |
| 101 | void cr_bind_sealed_sink(struct cr_sink *out, struct cr_sealed_sink *ctx) |
| 102 | { |
| 103 | out->ctx = ctx; |
| 104 | out->transact = sealed_transact; |
| 105 | } |