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/aienux/blob/main/native/m5/m5_envelope.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / m5 / m5_envelope.c` 212 lines ยท 9203 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 /* M5 chunked AES-256-GCM-SIV object envelope. Byte-compatible port of 18 * crates/aienos-kernel/src/crypto/envelope.rs (V1 header and chunk AAD). 19 * 20 * Header (64 bytes, little-endian), as EnvelopeHeader::encode: 21 * 0 magic "AIENENV1" | 8 version u16 = 1 | 10 cipher_suite u16 = 1 22 * 12 flags u32 = 0 | 16 chunk_size u32 | 20 total_plaintext_len u64 23 * 28 envelope_id[16] | 44 nonce_prefix[8] | 52 key_epoch u64 | 60 reserved u32 = 0 24 * Body: chunks of (ciphertext || 16-byte tag); an empty plaintext is one 25 * chunk holding only a tag. Chunk nonce: nonce_prefix[8] || chunk_index u32le. 26 * Chunk AAD (AGENTS.md section 3, 111 bytes): "AIENOS-M5-CHUNK-V1\0" || 27 * store_uuid || kind u16 || version u16 || header[64] || chunk_index u32 || 28 * chunk_plaintext_length u32. 29 * No ObjectId and no Store generation enter the AAD or the key. Truncation 30 * and extension are refused because total_plaintext_len is in every chunk's 31 * AAD and the length must match it exactly. 32 * Chunk key: m5_derive_object_key (store, kind, version, envelope_id, 33 * key generation, identity class), so a chunk from another envelope fails. 34 */ 35 #include "m5_internal.h" 36 37 #define CHUNK_AAD_LABEL "AIENOS-M5-CHUNK-V1" 38 #define CHUNK_AAD_LEN (sizeof(CHUNK_AAD_LABEL) + 16 + 2 + 2 + M5_ENV_HEADER_LEN + 4 + 4) 39 _Static_assert(CHUNK_AAD_LEN == 111, "chunk AAD must match envelope.rs CHUNK_AAD_LEN"); 40 41 static uint64_t chunks_for(uint64_t pt_len, uint32_t chunk_size) 42 { 43 return pt_len == 0 ? 1 : pt_len / chunk_size + (pt_len % chunk_size != 0); 44 } 45 46 size_t m5_envelope_len(uint64_t pt_len, uint32_t chunk_size) 47 { 48 if (chunk_size == 0 || chunk_size > M5_ENV_MAX_CHUNK) return 0; 49 if (pt_len > M5_ENV_MAX_PLAINTEXT) return 0; 50 uint64_t chunks = chunks_for(pt_len, chunk_size); 51 if (chunks > M5_ENV_MAX_CHUNKS) return 0; 52 uint64_t len = M5_ENV_HEADER_LEN + pt_len + chunks * M5_TAG_LEN; 53 if (len > SIZE_MAX) return 0; 54 return (size_t)len; 55 } 56 57 static void header_encode(const m5_env_header *h, uint8_t out[M5_ENV_HEADER_LEN]) 58 { 59 memset(out, 0, M5_ENV_HEADER_LEN); 60 memcpy(out, "AIENENV1", 8); 61 m5_put16(out + 8, h->version); 62 m5_put16(out + 10, h->cipher_suite); 63 m5_put32(out + 12, h->flags); 64 m5_put32(out + 16, h->chunk_size); 65 m5_put64(out + 20, h->total_plaintext_len); 66 memcpy(out + 28, h->envelope_id, 16); 67 memcpy(out + 44, h->nonce_prefix, 8); 68 m5_put64(out + 52, h->key_epoch); 69 } 70 71 int m5_envelope_parse_header(const uint8_t *env, size_t env_len, m5_env_header *h) 72 { 73 if (!env || !h) return M5_ERR_ARG; 74 memset(h, 0, sizeof *h); 75 if (env_len < M5_ENV_HEADER_LEN) return M5_ERR_BOUNDS; 76 if (memcmp(env, "AIENENV1", 8) != 0) return M5_ERR_FORMAT; 77 h->version = m5_get16(env + 8); 78 h->cipher_suite = m5_get16(env + 10); 79 h->flags = m5_get32(env + 12); 80 h->chunk_size = m5_get32(env + 16); 81 h->total_plaintext_len = m5_get64(env + 20); 82 memcpy(h->envelope_id, env + 28, 16); 83 memcpy(h->nonce_prefix, env + 44, 8); 84 h->key_epoch = m5_get64(env + 52); 85 if (h->version != 1 || h->cipher_suite != 1 || h->flags != 0) return M5_ERR_FORMAT; 86 if (m5_get32(env + 60) != 0) return M5_ERR_FORMAT; 87 /* Bound every unauthenticated length before deriving anything from it. */ 88 if (h->chunk_size == 0 || h->chunk_size > M5_ENV_MAX_CHUNK) return M5_ERR_BOUNDS; 89 if (h->total_plaintext_len > M5_ENV_MAX_PLAINTEXT) return M5_ERR_BOUNDS; 90 uint64_t count = chunks_for(h->total_plaintext_len, h->chunk_size); 91 if (count > M5_ENV_MAX_CHUNKS) return M5_ERR_BOUNDS; 92 h->chunk_count = (uint32_t)count; 93 size_t want = m5_envelope_len(h->total_plaintext_len, h->chunk_size); 94 if (want == 0 || env_len != want) return M5_ERR_BOUNDS; /* GUARD:env-len */ 95 return M5_OK; 96 } 97 98 static void chunk_aad(const m5_object_binding *b, const uint8_t hdr[M5_ENV_HEADER_LEN], 99 uint32_t idx, uint32_t len, uint8_t aad[CHUNK_AAD_LEN]) 100 { 101 uint8_t *p = aad; 102 memcpy(p, CHUNK_AAD_LABEL, sizeof(CHUNK_AAD_LABEL)); p += sizeof(CHUNK_AAD_LABEL); 103 memcpy(p, b->store_uuid, 16); p += 16; 104 m5_put16(p, b->object_kind); p += 2; 105 m5_put16(p, b->object_version); p += 2; 106 memcpy(p, hdr, M5_ENV_HEADER_LEN); p += M5_ENV_HEADER_LEN; 107 m5_put32(p, idx); p += 4; 108 m5_put32(p, len); 109 } 110 111 static void chunk_nonce(const uint8_t prefix[8], uint32_t idx, uint8_t nonce[12]) 112 { 113 memcpy(nonce, prefix, 8); 114 m5_put32(nonce + 8, idx); 115 } 116 117 int m5_envelope_seal(const uint8_t domain_key[32], const m5_object_binding *b, 118 const uint8_t nonce_prefix[8], uint32_t chunk_size, 119 const uint8_t *pt, size_t pt_len, 120 uint8_t *out, size_t out_cap, size_t *out_len) 121 { 122 if (out_len) *out_len = 0; 123 if (!domain_key || !b || !nonce_prefix || !out || !out_len || (!pt && pt_len)) return M5_ERR_ARG; 124 if (!m5_valid_class(b->identity_class)) return M5_ERR_ARG; 125 size_t total = m5_envelope_len(pt_len, chunk_size); 126 if (total == 0) return M5_ERR_BOUNDS; 127 if (out_cap < total) return M5_ERR_SPACE; 128 129 m5_env_header h; 130 memset(&h, 0, sizeof h); 131 h.version = 1; 132 h.cipher_suite = 1; 133 h.chunk_size = chunk_size; 134 h.total_plaintext_len = pt_len; 135 memcpy(h.envelope_id, b->envelope_id, 16); 136 memcpy(h.nonce_prefix, nonce_prefix, 8); 137 h.key_epoch = b->key_generation; 138 h.chunk_count = (uint32_t)chunks_for(pt_len, chunk_size); 139 header_encode(&h, out); 140 141 uint8_t key[32]; 142 m5_derive_object_key(domain_key, b, key); 143 size_t off = M5_ENV_HEADER_LEN, pos = 0; 144 int rc = M5_OK; 145 for (uint32_t i = 0; i < h.chunk_count; i++) { 146 size_t n = pt_len - pos < chunk_size ? pt_len - pos : chunk_size; 147 uint8_t aad[CHUNK_AAD_LEN], nonce[12]; 148 chunk_aad(b, out, i, (uint32_t)n, aad); 149 chunk_nonce(nonce_prefix, i, nonce); 150 if (aienos_gcmsiv_seal(key, nonce, aad, sizeof aad, n ? pt + pos : NULL, n, 151 out + off, n + M5_TAG_LEN) != AIENOS_CRYPTO_OK) { 152 rc = M5_ERR_ARG; 153 break; 154 } 155 pos += n; 156 off += n + M5_TAG_LEN; 157 } 158 aienos_wipe(key, sizeof key); 159 if (rc != M5_OK) { aienos_wipe(out, total); return rc; } 160 *out_len = total; 161 return M5_OK; 162 } 163 164 int m5_envelope_open(const uint8_t domain_key[32], uint8_t mode, const m5_object_binding *expect, 165 const uint8_t *env, size_t env_len, 166 uint8_t *out, size_t out_cap, size_t *out_len) 167 { 168 /* All-or-nothing: the caller's buffer is zero on every error path. */ 169 if (out && out_cap) aienos_wipe(out, out_cap); 170 if (out_len) *out_len = 0; 171 if (!domain_key || !expect || !env || !out_len || (!out && out_cap)) return M5_ERR_ARG; 172 173 m5_env_header h; 174 int rc = m5_envelope_parse_header(env, env_len, &h); 175 if (rc != M5_OK) return rc; 176 if (expect->identity_class != mode) return M5_ERR_IDENTITY; /* GUARD:env-mode */ 177 /* Authenticated header fields (they are in every chunk's AAD) must name 178 * the envelope the caller expects. */ 179 if (memcmp(h.envelope_id, expect->envelope_id, 16) != 0) return M5_ERR_BINDING; /* GUARD:env-envid */ 180 if (h.key_epoch != expect->key_generation) return M5_ERR_BINDING; /* GUARD:env-keygen */ 181 if (out_cap < h.total_plaintext_len) return M5_ERR_SPACE; 182 183 /* Private scratchpad: a chunk is decrypted here, its tag checked, and only 184 * then copied to the caller (AGENTS.md section 4). Unauthenticated 185 * plaintext never reaches the caller buffer. */ 186 uint8_t key[32], scratch[M5_ENV_MAX_CHUNK]; 187 m5_derive_object_key(domain_key, expect, key); 188 size_t off = M5_ENV_HEADER_LEN, pos = 0; 189 for (uint32_t i = 0; i < h.chunk_count; i++) { 190 size_t n = h.total_plaintext_len - pos < h.chunk_size ? h.total_plaintext_len - pos : h.chunk_size; 191 uint8_t aad[CHUNK_AAD_LEN], nonce[12]; 192 chunk_aad(expect, env, i, (uint32_t)n, aad); 193 chunk_nonce(h.nonce_prefix, i, nonce); 194 if (aienos_gcmsiv_open(key, nonce, aad, sizeof aad, env + off, n + M5_TAG_LEN, 195 scratch, n) != AIENOS_CRYPTO_OK) { 196 rc = M5_ERR_AUTH; 197 break; 198 } 199 if (n) memcpy(out + pos, scratch, n); 200 pos += n; 201 off += n + M5_TAG_LEN; 202 } 203 aienos_wipe(scratch, sizeof scratch); 204 aienos_wipe(key, sizeof key); 205 if (rc != M5_OK) { 206 /* A verified prefix never becomes partial success. */ 207 aienos_wipe(out, out_cap); /* GUARD:wipe-all */ 208 return rc; 209 } 210 *out_len = (size_t)h.total_plaintext_len; 211 return M5_OK; 212 }