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 / m5 / m5_owner_sig.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 | /* M5 owner-signed migration record: Ed25519 (native/sig) over a 208-byte |
| 18 | * body that binds source and destination store, agent root, source store |
| 19 | * generation, store format version, the set of migrated envelope ObjectIds, |
| 20 | * the MAC'd migration manifest and a monotonic migration counter. |
| 21 | * No heap, clock or I/O. Layout: m5.h. */ |
| 22 | #include "m5_internal.h" |
| 23 | #include "../argus/sha256.h" |
| 24 | #include "../sig/aienos_sig.h" |
| 25 | |
| 26 | #define OMG_DOMAIN "AIENOS-M5-OWNER-MIGRATION-V1" |
| 27 | #define ENVSET_DOMAIN "AIENOS-M5-ENVSET-V1" |
| 28 | #define OMG_SIG_OFF M5_OWNER_MIG_BODY_LEN |
| 29 | |
| 30 | int m5_envelope_set_digest(const uint8_t (*ids)[32], uint32_t n, uint8_t out[32]) |
| 31 | { |
| 32 | if (!out || (!ids && n)) return M5_ERR_ARG; |
| 33 | for (uint32_t i = 1; i < n; i++) { |
| 34 | if (memcmp(ids[i - 1], ids[i], 32) >= 0) return M5_ERR_FORMAT; /* GUARD:osig-envset-order */ |
| 35 | } |
| 36 | uint8_t cnt[4]; |
| 37 | m5_put32(cnt, n); |
| 38 | sha256_ctx c; |
| 39 | sha256_init(&c); |
| 40 | sha256_update(&c, (const uint8_t *)ENVSET_DOMAIN, sizeof(ENVSET_DOMAIN)); |
| 41 | sha256_update(&c, cnt, 4); |
| 42 | for (uint32_t i = 0; i < n; i++) sha256_update(&c, ids[i], 32); |
| 43 | sha256_final(&c, out); |
| 44 | return M5_OK; |
| 45 | } |
| 46 | |
| 47 | static void omg_encode(const m5_owner_migration *m, const uint8_t key_id[32], uint8_t *out) |
| 48 | { |
| 49 | memset(out, 0, M5_OWNER_MIG_LEN); |
| 50 | memcpy(out, "AIENOMG1", 8); |
| 51 | m5_put16(out + 8, 1); |
| 52 | out[12] = m->identity_class; |
| 53 | memcpy(out + 16, key_id, 32); |
| 54 | memcpy(out + 48, m->agent_root_id, 32); |
| 55 | memcpy(out + 80, m->source_store_uuid, 16); |
| 56 | memcpy(out + 96, m->dest_store_uuid, 16); |
| 57 | m5_put64(out + 112, m->source_store_generation); |
| 58 | m5_put32(out + 120, m->store_format_version); |
| 59 | m5_put32(out + 124, m->envelope_count); |
| 60 | memcpy(out + 128, m->envelope_set_digest, 32); |
| 61 | memcpy(out + 160, m->migration_manifest_digest, 32); |
| 62 | m5_put64(out + 192, m->migration_counter); |
| 63 | m5_put64(out + 200, m->owner_hierarchy_generation); |
| 64 | } |
| 65 | |
| 66 | static void omg_decode(const uint8_t *b, m5_owner_migration *m) |
| 67 | { |
| 68 | m->identity_class = b[12]; |
| 69 | memcpy(m->owner_key_id, b + 16, 32); |
| 70 | memcpy(m->agent_root_id, b + 48, 32); |
| 71 | memcpy(m->source_store_uuid, b + 80, 16); |
| 72 | memcpy(m->dest_store_uuid, b + 96, 16); |
| 73 | m->source_store_generation = m5_get64(b + 112); |
| 74 | m->store_format_version = m5_get32(b + 120); |
| 75 | m->envelope_count = m5_get32(b + 124); |
| 76 | memcpy(m->envelope_set_digest, b + 128, 32); |
| 77 | memcpy(m->migration_manifest_digest, b + 160, 32); |
| 78 | m->migration_counter = m5_get64(b + 192); |
| 79 | m->owner_hierarchy_generation = m5_get64(b + 200); |
| 80 | } |
| 81 | |
| 82 | /* Magic, version, flags, reserved bytes and class. */ |
| 83 | static int omg_check_format(const uint8_t *b) |
| 84 | { |
| 85 | if (memcmp(b, "AIENOMG1", 8) != 0) return M5_ERR_FORMAT; /* GUARD:osig-magic */ |
| 86 | if (m5_get16(b + 8) != 1 || m5_get16(b + 10) != 0) return M5_ERR_FORMAT; /* GUARD:osig-version */ |
| 87 | if (!m5_all_zero(b + 13, 3)) return M5_ERR_FORMAT; /* GUARD:osig-reserved */ |
| 88 | if (!m5_valid_class(b[12])) return M5_ERR_FORMAT; |
| 89 | return M5_OK; |
| 90 | } |
| 91 | |
| 92 | /* Signed message: domain (with its NUL) || body[0..208]. */ |
| 93 | #define OMG_MSG_LEN (sizeof(OMG_DOMAIN) + M5_OWNER_MIG_BODY_LEN) |
| 94 | static void omg_message(const uint8_t *rec, uint8_t msg[OMG_MSG_LEN]) |
| 95 | { |
| 96 | memcpy(msg, OMG_DOMAIN, sizeof(OMG_DOMAIN)); |
| 97 | memcpy(msg + sizeof(OMG_DOMAIN), rec, M5_OWNER_MIG_BODY_LEN); |
| 98 | } |
| 99 | |
| 100 | int m5_owner_migration_body(const m5_owner_migration *m, const uint8_t owner_pk[32], |
| 101 | uint8_t out[M5_OWNER_MIG_LEN]) |
| 102 | { |
| 103 | if (!m || !owner_pk || !out) return M5_ERR_ARG; |
| 104 | if (!m5_valid_class(m->identity_class)) return M5_ERR_ARG; |
| 105 | uint8_t key_id[32]; |
| 106 | sha256_hash(owner_pk, 32, key_id); |
| 107 | omg_encode(m, key_id, out); |
| 108 | return M5_OK; |
| 109 | } |
| 110 | |
| 111 | int m5_owner_migration_sign_body(uint8_t rec[M5_OWNER_MIG_LEN], const uint8_t owner_sk[32]) |
| 112 | { |
| 113 | if (!rec || !owner_sk) return M5_ERR_ARG; |
| 114 | int rc = omg_check_format(rec); |
| 115 | if (rc != M5_OK) return rc; |
| 116 | if (!m5_all_zero(rec + OMG_SIG_OFF, 64)) return M5_ERR_FORMAT; /* already signed */ |
| 117 | uint8_t pk[32], key_id[32], msg[OMG_MSG_LEN]; |
| 118 | if (aienos_ed25519_public_key(pk, owner_sk) != AIENOS_SIG_OK) return M5_ERR_ARG; |
| 119 | sha256_hash(pk, 32, key_id); |
| 120 | if (!aienos_ct_equal(key_id, rec + 16, 32)) return M5_ERR_BINDING; |
| 121 | omg_message(rec, msg); |
| 122 | if (aienos_ed25519_sign(rec + OMG_SIG_OFF, msg, sizeof msg, owner_sk) != AIENOS_SIG_OK) { |
| 123 | aienos_wipe(rec + OMG_SIG_OFF, 64); |
| 124 | return M5_ERR_ARG; |
| 125 | } |
| 126 | return M5_OK; |
| 127 | } |
| 128 | |
| 129 | int m5_owner_migration_sign(const m5_owner_migration *m, const uint8_t owner_sk[32], |
| 130 | uint8_t out[M5_OWNER_MIG_LEN]) |
| 131 | { |
| 132 | if (!m || !owner_sk || !out) return M5_ERR_ARG; |
| 133 | uint8_t pk[32]; |
| 134 | if (aienos_ed25519_public_key(pk, owner_sk) != AIENOS_SIG_OK) return M5_ERR_ARG; |
| 135 | int rc = m5_owner_migration_body(m, pk, out); |
| 136 | if (rc == M5_OK) rc = m5_owner_migration_sign_body(out, owner_sk); |
| 137 | if (rc != M5_OK) memset(out, 0, M5_OWNER_MIG_LEN); |
| 138 | return rc; |
| 139 | } |
| 140 | |
| 141 | int m5_owner_migration_verify(const uint8_t *b, size_t len, const uint8_t owner_pk[32], |
| 142 | uint8_t mode, const m5_owner_migration *e, |
| 143 | uint64_t last_migration_counter, m5_owner_migration *m) |
| 144 | { |
| 145 | if (!b || !owner_pk || !e || !m) return M5_ERR_ARG; |
| 146 | memset(m, 0, sizeof *m); |
| 147 | if (len != M5_OWNER_MIG_LEN) return M5_ERR_BOUNDS; /* GUARD:osig-len */ |
| 148 | int rc = omg_check_format(b); |
| 149 | if (rc != M5_OK) return rc; |
| 150 | if (b[12] != mode || e->identity_class != mode) return M5_ERR_IDENTITY; /* GUARD:osig-class */ |
| 151 | uint8_t key_id[32], msg[OMG_MSG_LEN]; |
| 152 | sha256_hash(owner_pk, 32, key_id); |
| 153 | if (!aienos_ct_equal(key_id, b + 16, 32)) return M5_ERR_AUTH; /* GUARD:osig-keyid */ |
| 154 | omg_message(b, msg); |
| 155 | if (aienos_ed25519_verify(b + OMG_SIG_OFF, msg, sizeof msg, owner_pk) != AIENOS_SIG_OK) return M5_ERR_AUTH; /* GUARD:osig-sig */ |
| 156 | omg_decode(b, m); |
| 157 | if (rc == M5_OK && memcmp(m->source_store_uuid, m->dest_store_uuid, 16) == 0) rc = M5_ERR_BINDING; /* GUARD:osig-self */ |
| 158 | if (rc == M5_OK && memcmp(m->source_store_uuid, e->source_store_uuid, 16) != 0) rc = M5_ERR_BINDING; /* GUARD:osig-source */ |
| 159 | if (rc == M5_OK && memcmp(m->dest_store_uuid, e->dest_store_uuid, 16) != 0) rc = M5_ERR_BINDING; /* GUARD:osig-dest */ |
| 160 | if (rc == M5_OK && memcmp(m->agent_root_id, e->agent_root_id, 32) != 0) rc = M5_ERR_BINDING; /* GUARD:osig-agent */ |
| 161 | if (rc == M5_OK && m->source_store_generation != e->source_store_generation) rc = M5_ERR_BINDING; /* GUARD:osig-gen */ |
| 162 | if (rc == M5_OK && m->store_format_version != e->store_format_version) rc = M5_ERR_BINDING; /* GUARD:osig-format */ |
| 163 | if (rc == M5_OK && m->envelope_count != e->envelope_count) rc = M5_ERR_BINDING; /* GUARD:osig-envcount */ |
| 164 | if (rc == M5_OK && !aienos_ct_equal(m->envelope_set_digest, e->envelope_set_digest, 32)) rc = M5_ERR_BINDING; /* GUARD:osig-envset */ |
| 165 | if (rc == M5_OK && !aienos_ct_equal(m->migration_manifest_digest, e->migration_manifest_digest, 32)) rc = M5_ERR_BINDING; /* GUARD:osig-manifest */ |
| 166 | if (rc == M5_OK && m->owner_hierarchy_generation != e->owner_hierarchy_generation) rc = M5_ERR_BINDING; /* GUARD:osig-ownergen */ |
| 167 | if (rc == M5_OK && m->migration_counter <= last_migration_counter) rc = M5_ERR_REPLAY; /* GUARD:osig-replay */ |
| 168 | if (rc != M5_OK) memset(m, 0, sizeof *m); |
| 169 | return rc; |
| 170 | } |
| 171 | |
| 172 | int m5_migration_authorize_owner(const uint8_t *mig_buf, size_t mig_len, |
| 173 | const uint8_t k_root_auth[32], |
| 174 | const uint8_t *own_buf, size_t own_len, |
| 175 | const uint8_t owner_pk[32], uint8_t mode, |
| 176 | const m5_migration *em, const m5_owner_migration *eo, |
| 177 | uint64_t last_migration_counter, |
| 178 | m5_migration *out_mig, m5_owner_migration *out_own) |
| 179 | { |
| 180 | if (!mig_buf || !k_root_auth || !own_buf || !owner_pk || !em || !eo || !out_mig || !out_own) |
| 181 | return M5_ERR_ARG; |
| 182 | memset(out_mig, 0, sizeof *out_mig); |
| 183 | memset(out_own, 0, sizeof *out_own); |
| 184 | int rc = m5_migration_authorize(mig_buf, mig_len, k_root_auth, mode, em, |
| 185 | last_migration_counter, out_mig); |
| 186 | if (rc != M5_OK) return rc; |
| 187 | /* The owner record must name this exact manifest and agree with it. */ |
| 188 | m5_owner_migration e; |
| 189 | memset(&e, 0, sizeof e); |
| 190 | e.identity_class = out_mig->identity_class; |
| 191 | memcpy(e.agent_root_id, out_mig->agent_root_id, 32); |
| 192 | memcpy(e.source_store_uuid, out_mig->source_store_uuid, 16); |
| 193 | memcpy(e.dest_store_uuid, out_mig->dest_store_uuid, 16); |
| 194 | e.source_store_generation = out_mig->source_store_generation; |
| 195 | e.store_format_version = eo->store_format_version; |
| 196 | e.envelope_count = eo->envelope_count; |
| 197 | memcpy(e.envelope_set_digest, eo->envelope_set_digest, 32); |
| 198 | sha256_hash(mig_buf, M5_MIGRATION_LEN, e.migration_manifest_digest); /* GUARD:osig-bind-manifest */ |
| 199 | e.owner_hierarchy_generation = out_mig->owner_hierarchy_generation; |
| 200 | rc = m5_owner_migration_verify(own_buf, own_len, owner_pk, mode, &e, |
| 201 | last_migration_counter, out_own); |
| 202 | if (rc == M5_OK && out_own->migration_counter != out_mig->migration_counter) rc = M5_ERR_BINDING; /* GUARD:osig-bind-counter */ |
| 203 | if (rc != M5_OK) { |
| 204 | memset(out_mig, 0, sizeof *out_mig); |
| 205 | memset(out_own, 0, sizeof *out_own); |
| 206 | } |
| 207 | return rc; |
| 208 | } |