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_keys.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 key hierarchy: HKDF-Expand, subkeys, per-object keys, keyslots, |
| 18 | * SecurityManifest. Port of security.rs sections 1-3. */ |
| 19 | #include "m5_internal.h" |
| 20 | |
| 21 | void m5_mac(const uint8_t key[32], const char *domain, size_t domain_len, |
| 22 | const uint8_t *body, size_t body_len, uint8_t out[32]) |
| 23 | { |
| 24 | aienos_hmac_sha256_ctx c; |
| 25 | aienos_hmac_sha256_init(&c, key); |
| 26 | aienos_hmac_sha256_update(&c, (const uint8_t *)domain, domain_len); |
| 27 | aienos_hmac_sha256_update(&c, body, body_len); |
| 28 | aienos_hmac_sha256_final(&c, out); |
| 29 | } |
| 30 | |
| 31 | int m5_hkdf_expand(const uint8_t prk[32], const uint8_t *info, size_t info_len, |
| 32 | uint8_t *out, size_t out_len) |
| 33 | { |
| 34 | if (!prk || (!info && info_len) || (!out && out_len)) return M5_ERR_ARG; |
| 35 | if (out_len > 255 * 32) return M5_ERR_BOUNDS; |
| 36 | uint8_t t[32]; |
| 37 | size_t t_len = 0, written = 0; |
| 38 | for (unsigned counter = 1; written < out_len; counter++) { |
| 39 | aienos_hmac_sha256_ctx c; |
| 40 | uint8_t ctr = (uint8_t)counter; |
| 41 | aienos_hmac_sha256_init(&c, prk); |
| 42 | aienos_hmac_sha256_update(&c, t, t_len); |
| 43 | aienos_hmac_sha256_update(&c, info, info_len); |
| 44 | aienos_hmac_sha256_update(&c, &ctr, 1); |
| 45 | aienos_hmac_sha256_final(&c, t); |
| 46 | t_len = 32; |
| 47 | size_t n = out_len - written < 32 ? out_len - written : 32; |
| 48 | memcpy(out + written, t, n); |
| 49 | written += n; |
| 50 | } |
| 51 | aienos_wipe(t, sizeof t); |
| 52 | return M5_OK; |
| 53 | } |
| 54 | |
| 55 | #define LBL(s) (const uint8_t *)(s), sizeof(s) - 1 |
| 56 | |
| 57 | void m5_derive_subkeys_v1(const uint8_t k_vol[32], m5_subkeys *out) |
| 58 | { |
| 59 | m5_hkdf_expand(k_vol, LBL("AIENOS/M5/CORTEX-V1"), out->k_cortex, 32); |
| 60 | m5_hkdf_expand(k_vol, LBL("AIENOS/M5/AGENT-STATE-V1"), out->k_agent, 32); |
| 61 | m5_hkdf_expand(k_vol, LBL("AIENOS/M5/ARTIFACT-V1"), out->k_artifact, 32); |
| 62 | m5_hkdf_expand(k_vol, LBL("AIENOS/M5/ROOT-AUTH-V1"), out->k_root_auth, 32); |
| 63 | } |
| 64 | |
| 65 | #define OWNER_LABEL "AIENOS/M5/OWNER-HIERARCHY-V2" |
| 66 | |
| 67 | int m5_derive_subkeys(const uint8_t k_vol[32], uint8_t identity_class, |
| 68 | uint64_t owner_hierarchy_generation, m5_subkeys *out) |
| 69 | { |
| 70 | if (!k_vol || !out) return M5_ERR_ARG; |
| 71 | if (!m5_valid_class(identity_class)) { aienos_wipe(out, sizeof *out); return M5_ERR_ARG; } |
| 72 | uint8_t info[sizeof(OWNER_LABEL) + 1 + 8]; |
| 73 | memset(info, 0, sizeof info); |
| 74 | memcpy(info, OWNER_LABEL, sizeof(OWNER_LABEL)); /* includes the NUL separator */ |
| 75 | info[sizeof(OWNER_LABEL)] = identity_class; /* GUARD:class-in-kdf */ |
| 76 | m5_put64(info + sizeof(OWNER_LABEL) + 1, owner_hierarchy_generation); |
| 77 | uint8_t k_class[32]; |
| 78 | m5_hkdf_expand(k_vol, info, sizeof info, k_class, 32); |
| 79 | m5_derive_subkeys_v1(k_class, out); |
| 80 | aienos_wipe(k_class, sizeof k_class); |
| 81 | return M5_OK; |
| 82 | } |
| 83 | |
| 84 | void m5_subkeys_wipe(m5_subkeys *k) { aienos_wipe(k, sizeof *k); } |
| 85 | |
| 86 | #define OBJECT_LABEL "AIENOS/M5/OBJECT-KEY-V2" |
| 87 | |
| 88 | int m5_derive_object_key(const uint8_t domain_key[32], const m5_object_binding *b, uint8_t out[32]) |
| 89 | { |
| 90 | if (!domain_key || !b || !out) return M5_ERR_ARG; |
| 91 | uint8_t info[sizeof(OBJECT_LABEL) + 16 + 2 + 2 + 16 + 8 + 1]; |
| 92 | uint8_t *p = info; |
| 93 | memcpy(p, OBJECT_LABEL, sizeof(OBJECT_LABEL)); p += sizeof(OBJECT_LABEL); |
| 94 | memcpy(p, b->store_uuid, 16); p += 16; |
| 95 | m5_put16(p, b->object_kind); p += 2; |
| 96 | m5_put16(p, b->object_version); p += 2; |
| 97 | memcpy(p, b->envelope_id, 16); p += 16; |
| 98 | m5_put64(p, b->key_generation); p += 8; |
| 99 | *p = b->identity_class; |
| 100 | return m5_hkdf_expand(domain_key, info, sizeof info, out, 32); |
| 101 | } |
| 102 | |
| 103 | /* ---- keyslots ---- */ |
| 104 | |
| 105 | void m5_keyslot_encode(const m5_keyslot *s, uint8_t out[M5_KEYSLOT_LEN]) |
| 106 | { |
| 107 | memset(out, 0, M5_KEYSLOT_LEN); |
| 108 | out[0] = s->slot_type; out[1] = s->wrap_suite; out[2] = s->kdf_suite; out[3] = s->flags; |
| 109 | m5_put32(out + 4, s->slot_id); |
| 110 | m5_put64(out + 8, s->key_epoch); |
| 111 | memcpy(out + 16, s->salt, 16); |
| 112 | m5_put32(out + 32, s->argon_m_kib); |
| 113 | m5_put32(out + 36, s->argon_t_cost); |
| 114 | m5_put32(out + 40, s->argon_p_cost); |
| 115 | memcpy(out + 44, s->wrap_nonce, 12); |
| 116 | memcpy(out + 56, s->wrapped_k_vol, 32); |
| 117 | memcpy(out + 88, s->wrap_tag, 16); |
| 118 | m5_put32(out + 104, s->payload_offset); |
| 119 | m5_put32(out + 108, s->payload_length); |
| 120 | } |
| 121 | |
| 122 | /* Value checks shared by decode and unwrap, so a struct built in memory |
| 123 | * cannot bypass what the serialized form would be refused for. */ |
| 124 | static int keyslot_check(const m5_keyslot *s) |
| 125 | { |
| 126 | if (s->slot_type > M5_SLOT_RECOVERY_RAW_SECRET) return M5_ERR_FORMAT; |
| 127 | if (s->argon_m_kib > 131072 || s->argon_t_cost > 10 || s->argon_p_cost > 16) return M5_ERR_BOUNDS; |
| 128 | /* ADR 0017: wrap_suite 0x01 only (an Empty slot may hold 0), kdf_suite |
| 129 | * 0x00..0x02, no flag bits defined. */ |
| 130 | if (s->slot_type != M5_SLOT_EMPTY && s->wrap_suite != M5_WRAP_SUITE_GCMSIV) return M5_ERR_FORMAT; /* GUARD:slot-suite */ |
| 131 | if (s->slot_type == M5_SLOT_EMPTY && s->wrap_suite > M5_WRAP_SUITE_GCMSIV) return M5_ERR_FORMAT; |
| 132 | if (s->kdf_suite > 0x02) return M5_ERR_FORMAT; /* GUARD:slot-kdf */ |
| 133 | if (s->flags != 0) return M5_ERR_FORMAT; /* GUARD:slot-flags */ |
| 134 | return M5_OK; |
| 135 | } |
| 136 | |
| 137 | int m5_keyslot_decode(const uint8_t *b, size_t len, m5_keyslot *s) |
| 138 | { |
| 139 | if (!b || !s) return M5_ERR_ARG; |
| 140 | if (len != M5_KEYSLOT_LEN) return M5_ERR_BOUNDS; |
| 141 | if (b[0] > M5_SLOT_RECOVERY_RAW_SECRET) return M5_ERR_FORMAT; |
| 142 | s->slot_type = b[0]; s->wrap_suite = b[1]; s->kdf_suite = b[2]; s->flags = b[3]; |
| 143 | s->slot_id = m5_get32(b + 4); |
| 144 | s->key_epoch = m5_get64(b + 8); |
| 145 | memcpy(s->salt, b + 16, 16); |
| 146 | s->argon_m_kib = m5_get32(b + 32); |
| 147 | s->argon_t_cost = m5_get32(b + 36); |
| 148 | s->argon_p_cost = m5_get32(b + 40); |
| 149 | if (keyslot_check(s) != M5_OK) return keyslot_check(s); |
| 150 | memcpy(s->wrap_nonce, b + 44, 12); |
| 151 | memcpy(s->wrapped_k_vol, b + 56, 32); |
| 152 | memcpy(s->wrap_tag, b + 88, 16); |
| 153 | s->payload_offset = m5_get32(b + 104); |
| 154 | s->payload_length = m5_get32(b + 108); |
| 155 | if (!m5_all_zero(b + 112, 16)) return M5_ERR_FORMAT; |
| 156 | return M5_OK; |
| 157 | } |
| 158 | |
| 159 | #define KEYSLOT_AAD_LABEL "AIENOS-M5-KEYSLOT-V2" |
| 160 | #define KEYSLOT_AAD_LEN (sizeof(KEYSLOT_AAD_LABEL) + 16 + 4 + 1 + 8) |
| 161 | |
| 162 | static void keyslot_aad(const m5_keyslot *s, const uint8_t store_uuid[16], uint8_t aad[KEYSLOT_AAD_LEN]) |
| 163 | { |
| 164 | uint8_t *p = aad; |
| 165 | memcpy(p, KEYSLOT_AAD_LABEL, sizeof(KEYSLOT_AAD_LABEL)); p += sizeof(KEYSLOT_AAD_LABEL); |
| 166 | memcpy(p, store_uuid, 16); p += 16; |
| 167 | m5_put32(p, s->slot_id); p += 4; |
| 168 | *p++ = s->slot_type; |
| 169 | m5_put64(p, s->key_epoch); |
| 170 | } |
| 171 | |
| 172 | int m5_keyslot_wrap(m5_keyslot *s, const uint8_t store_uuid[16], const uint8_t kek[32], |
| 173 | const uint8_t k_vol[32], const uint8_t nonce[12]) |
| 174 | { |
| 175 | if (!s || !store_uuid || !kek || !k_vol || !nonce) return M5_ERR_ARG; |
| 176 | if (s->slot_type == M5_SLOT_EMPTY || s->slot_type > M5_SLOT_RECOVERY_RAW_SECRET) return M5_ERR_FORMAT; |
| 177 | uint8_t aad[KEYSLOT_AAD_LEN], ct[48]; |
| 178 | s->wrap_suite = M5_WRAP_SUITE_GCMSIV; |
| 179 | memcpy(s->wrap_nonce, nonce, 12); |
| 180 | keyslot_aad(s, store_uuid, aad); |
| 181 | if (aienos_gcmsiv_seal(kek, nonce, aad, sizeof aad, k_vol, 32, ct, sizeof ct) != AIENOS_CRYPTO_OK) |
| 182 | return M5_ERR_ARG; |
| 183 | memcpy(s->wrapped_k_vol, ct, 32); |
| 184 | memcpy(s->wrap_tag, ct + 32, 16); |
| 185 | return M5_OK; |
| 186 | } |
| 187 | |
| 188 | int m5_keyslot_unwrap(const m5_keyslot *s, const uint8_t store_uuid[16], |
| 189 | const uint8_t kek[32], uint8_t k_vol_out[32]) |
| 190 | { |
| 191 | if (!k_vol_out) return M5_ERR_ARG; |
| 192 | memset(k_vol_out, 0, 32); |
| 193 | if (!s || !store_uuid || !kek) return M5_ERR_ARG; |
| 194 | if (s->slot_type == M5_SLOT_EMPTY) return M5_ERR_FORMAT; /* GUARD:slot-empty */ |
| 195 | if (keyslot_check(s) != M5_OK) return keyslot_check(s); /* GUARD:slot-check */ |
| 196 | if (s->wrap_suite != M5_WRAP_SUITE_GCMSIV) return M5_ERR_FORMAT; |
| 197 | uint8_t aad[KEYSLOT_AAD_LEN], ct[48]; |
| 198 | keyslot_aad(s, store_uuid, aad); |
| 199 | memcpy(ct, s->wrapped_k_vol, 32); |
| 200 | memcpy(ct + 32, s->wrap_tag, 16); |
| 201 | if (aienos_gcmsiv_open(kek, s->wrap_nonce, aad, sizeof aad, ct, sizeof ct, k_vol_out, 32) != AIENOS_CRYPTO_OK) { |
| 202 | aienos_wipe(k_vol_out, 32); |
| 203 | return M5_ERR_AUTH; |
| 204 | } |
| 205 | return M5_OK; |
| 206 | } |
| 207 | |
| 208 | /* ---- SecurityManifest: Rust v1 layout (security.rs), 256 bytes ---- |
| 209 | * 0 magic "AIENSEC1" | 8 format_version u16 = 1 | 10 flags u16 = 0 | 12 reserved u32 = 0 |
| 210 | * | 16 store_uuid | 32 generation | 40 security_sequence | 48 epoch | 56 key_epoch |
| 211 | * | 64 prev id | 96 keyslot id | 128 agent_root id | 160 continuity id |
| 212 | * | 192 migration id | 224 root_mac (HMAC(k_root_auth, "AIENOS-M5-ROOT-AUTH-V1\0" || bytes[0..224])) |
| 213 | * Identity class and owner hierarchy generation are bound through k_root_auth. */ |
| 214 | #define SECMAN_MAC_OFF 224 |
| 215 | #define ROOT_AUTH_DOMAIN "AIENOS-M5-ROOT-AUTH-V1" |
| 216 | |
| 217 | static void secman_encode_body(const m5_secman *m, uint8_t out[M5_SECMAN_LEN]) |
| 218 | { |
| 219 | memset(out, 0, M5_SECMAN_LEN); |
| 220 | memcpy(out, "AIENSEC1", 8); |
| 221 | m5_put16(out + 8, 1); |
| 222 | memcpy(out + 16, m->store_uuid, 16); |
| 223 | m5_put64(out + 32, m->generation); |
| 224 | m5_put64(out + 40, m->security_sequence); |
| 225 | m5_put64(out + 48, m->epoch); |
| 226 | m5_put64(out + 56, m->key_epoch); |
| 227 | memcpy(out + 64, m->previous_security_manifest_id, 32); |
| 228 | memcpy(out + 96, m->keyslot_manifest_id, 32); |
| 229 | memcpy(out + 128, m->agent_root_id, 32); |
| 230 | memcpy(out + 160, m->continuity_manifest_id, 32); |
| 231 | memcpy(out + 192, m->migration_manifest_id, 32); |
| 232 | } |
| 233 | |
| 234 | int m5_secman_seal(m5_secman *m, const uint8_t k_root_auth[32], uint8_t out[M5_SECMAN_LEN]) |
| 235 | { |
| 236 | if (!m || !k_root_auth || !out) return M5_ERR_ARG; |
| 237 | secman_encode_body(m, out); |
| 238 | m5_mac(k_root_auth, ROOT_AUTH_DOMAIN, sizeof(ROOT_AUTH_DOMAIN), out, SECMAN_MAC_OFF, m->root_mac); |
| 239 | memcpy(out + SECMAN_MAC_OFF, m->root_mac, 32); |
| 240 | return M5_OK; |
| 241 | } |
| 242 | |
| 243 | int m5_secman_open(const uint8_t *b, size_t len, const uint8_t k_root_auth[32], m5_secman *m) |
| 244 | { |
| 245 | if (!b || !k_root_auth || !m) return M5_ERR_ARG; |
| 246 | memset(m, 0, sizeof *m); |
| 247 | if (len != M5_SECMAN_LEN) return M5_ERR_BOUNDS; |
| 248 | if (memcmp(b, "AIENSEC1", 8) != 0 || m5_get16(b + 8) != 1 || m5_get16(b + 10) != 0) |
| 249 | return M5_ERR_FORMAT; |
| 250 | if (!m5_all_zero(b + 12, 4)) return M5_ERR_FORMAT; |
| 251 | uint8_t mac[32]; |
| 252 | m5_mac(k_root_auth, ROOT_AUTH_DOMAIN, sizeof(ROOT_AUTH_DOMAIN), b, SECMAN_MAC_OFF, mac); |
| 253 | int ok = aienos_ct_equal(mac, b + SECMAN_MAC_OFF, 32); |
| 254 | aienos_wipe(mac, sizeof mac); |
| 255 | if (!ok) return M5_ERR_AUTH; /* GUARD:secman-mac */ |
| 256 | memcpy(m->store_uuid, b + 16, 16); |
| 257 | m->generation = m5_get64(b + 32); |
| 258 | m->security_sequence = m5_get64(b + 40); |
| 259 | m->epoch = m5_get64(b + 48); |
| 260 | m->key_epoch = m5_get64(b + 56); |
| 261 | memcpy(m->previous_security_manifest_id, b + 64, 32); |
| 262 | memcpy(m->keyslot_manifest_id, b + 96, 32); |
| 263 | memcpy(m->agent_root_id, b + 128, 32); |
| 264 | memcpy(m->continuity_manifest_id, b + 160, 32); |
| 265 | memcpy(m->migration_manifest_id, b + 192, 32); |
| 266 | memcpy(m->root_mac, b + SECMAN_MAC_OFF, 32); |
| 267 | return M5_OK; |
| 268 | } |
| 269 | |
| 270 | /* ---- KeySlotManifest (kind 21) ---- */ |
| 271 | |
| 272 | void m5_ksm_encode(const m5_ksm *m, uint8_t out[M5_KSM_FIXED_LEN]) |
| 273 | { |
| 274 | memset(out, 0, M5_KSM_FIXED_LEN); |
| 275 | memcpy(out, "AIENKSL1", 8); |
| 276 | m5_put16(out + 8, 1); |
| 277 | m5_put16(out + 10, 0); |
| 278 | memcpy(out + 12, m->store_uuid, 16); |
| 279 | m5_put64(out + 28, m->manifest_sequence); |
| 280 | memcpy(out + 36, m->previous_keyslot_manifest_id, 32); |
| 281 | m5_put64(out + 68, m->active_key_epoch); |
| 282 | m5_put32(out + 76, m->slot_count); |
| 283 | for (int i = 0; i < M5_KSM_SLOTS; i++) m5_keyslot_encode(&m->slots[i], out + 128 + i * M5_KEYSLOT_LEN); |
| 284 | } |
| 285 | |
| 286 | int m5_ksm_decode(const uint8_t *b, size_t len, m5_ksm *m) |
| 287 | { |
| 288 | if (!b || !m) return M5_ERR_ARG; |
| 289 | memset(m, 0, sizeof *m); |
| 290 | if (len < M5_KSM_FIXED_LEN || len > M5_KSM_FIXED_LEN + M5_KSM_ARENA_MAX) return M5_ERR_BOUNDS; |
| 291 | if (memcmp(b, "AIENKSL1", 8) != 0) return M5_ERR_FORMAT; |
| 292 | if (m5_get16(b + 8) != 1) return M5_ERR_FORMAT; /* GUARD:ksm-version */ |
| 293 | if (m5_get16(b + 10) != 0 || !m5_all_zero(b + 80, 48)) return M5_ERR_FORMAT; /* GUARD:ksm-reserved */ |
| 294 | memcpy(m->store_uuid, b + 12, 16); |
| 295 | m->manifest_sequence = m5_get64(b + 28); |
| 296 | memcpy(m->previous_keyslot_manifest_id, b + 36, 32); |
| 297 | m->active_key_epoch = m5_get64(b + 68); |
| 298 | m->slot_count = m5_get32(b + 76); |
| 299 | if (m->slot_count > M5_KSM_SLOTS) return M5_ERR_BOUNDS; /* GUARD:ksm-slot-count */ |
| 300 | m->arena_len = (uint32_t)(len - M5_KSM_FIXED_LEN); |
| 301 | for (int i = 0; i < M5_KSM_SLOTS; i++) { |
| 302 | int rc = m5_keyslot_decode(b + 128 + i * M5_KEYSLOT_LEN, M5_KEYSLOT_LEN, &m->slots[i]); |
| 303 | if (rc != M5_OK) return rc; |
| 304 | const m5_keyslot *s = &m->slots[i]; |
| 305 | if ((uint64_t)s->payload_offset + s->payload_length > m->arena_len) return M5_ERR_BOUNDS; /* GUARD:ksm-payload-range */ |
| 306 | /* an unused descriptor past slot_count must be Empty */ |
| 307 | if ((uint32_t)i >= m->slot_count && s->slot_type != M5_SLOT_EMPTY) return M5_ERR_FORMAT; /* GUARD:ksm-unused-empty */ |
| 308 | } |
| 309 | return M5_OK; |
| 310 | } |
| 311 | |
| 312 | int m5_recovery_kek(const uint8_t salt[16], const uint8_t *secret, size_t secret_len, uint8_t out[32]) |
| 313 | { |
| 314 | if (out) memset(out, 0, 32); |
| 315 | if (!salt || !secret || !out || secret_len == 0) return M5_ERR_ARG; |
| 316 | /* HKDF-Extract: HMAC keyed by the 16-byte salt. HMAC zero-pads a short |
| 317 | * key, so a 32-byte key of salt || 0^16 is the same PRK as RFC 5869 with |
| 318 | * the bare 16-byte salt. */ |
| 319 | uint8_t k[32], prk[32]; |
| 320 | memset(k, 0, 32); |
| 321 | memcpy(k, salt, 16); |
| 322 | aienos_hmac_sha256(k, secret, secret_len, prk); |
| 323 | int rc = m5_hkdf_expand(prk, (const uint8_t *)M5_RECOVERY_KEK_INFO, sizeof(M5_RECOVERY_KEK_INFO) - 1, out, 32); |
| 324 | aienos_wipe(k, sizeof k); |
| 325 | aienos_wipe(prk, sizeof prk); |
| 326 | if (rc != M5_OK) memset(out, 0, 32); |
| 327 | return rc; |
| 328 | } |