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/kernel/tests/test_continuity_geometry.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 / kernel / tests / test_continuity_geometry.c` 355 lines ยท 13360 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 /* test_continuity_geometry.c -- host test of contract item K-6 (Region geometry, 18 * native/kernel/CONTINUITY_RECOVERY_CONTRACT.md K-6 and section 1.7, aienos#222, 19 * cut 7). Harness helpers are copied from test_continuity_recovery.c (not shared). 20 * 21 * Proves, on the REAL sealed Store in a file (anchor units 0-3, Store region 22 * after them, as dev/disk_layout.h): 23 * (1) rc state_digest == SHA-256 over Store-region units 0 and 1 (read straight 24 * from the image at offsets A_UNITS and A_UNITS+1), and is NOT the digest of 25 * the anchor units (image units 0 and 1); 26 * (2) flipping a byte in Store-region unit 0, and separately unit 1, changes the 27 * digest (and the challenge where a challenge exists); 28 * (3) flipping a byte in each anchor unit 0..3 leaves the digest unchanged; 29 * (4) the dev/disk_layout.h constants and the Store-region arithmetic for U units. 30 * Each check has a counterexample: the digest must DIFFER after a store flip, 31 * and must DIFFER from the anchor-unit digest, so a hardcoded value cannot pass. 32 * Status: written in cut 7, NOT_RUN until the forge reports. */ 33 #include 34 #include 35 #include 36 #include 37 #include 38 39 #include "ck_test.h" 40 #include "continuity_recovery.h" 41 #include "disk_file.h" 42 #include "disk_layout.h" 43 44 #define CK_(c, ...) \ 45 do { \ 46 ck_t_run++; \ 47 if (!(c)) { \ 48 ck_t_fail++; \ 49 printf(" FAIL %s:%d: %s: ", __FILE__, __LINE__, #c); \ 50 printf(__VA_ARGS__); \ 51 printf("\n"); \ 52 } \ 53 } while (0) 54 55 #define A_UNITS CK_LAYOUT_ANCHOR_UNITS 56 #define S_UNITS 512u 57 #define T_UNITS (A_UNITS + S_UNITS) 58 #define IMG_BYTES ((size_t)T_UNITS * 4096u) 59 60 static ss_workspace g_ws; 61 static ss_store g_s; 62 static disk_file g_f; 63 static disk_dev g_d; 64 static st_disk g_sd; 65 static st_dev g_sdev; 66 static ts_device g_tdev; 67 static ss_keys g_k; 68 static uint32_t g_bs; 69 static char g_path[256]; 70 static struct cr_work g_w; 71 static struct cr_txwork g_tw; 72 static struct cr_view g_v, g_v2; 73 static struct cr_source g_src; 74 static struct cr_sink g_snk; 75 static struct cr_sealed_sink g_sk; 76 static struct rc_env E; 77 static const uint8_t RUUID[16] = {0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 78 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a}; 79 static uint8_t img[IMG_BYTES]; 80 81 static int g_open; 82 static void dev_close(void) 83 { 84 if (g_open) disk_file_close(&g_f); 85 g_open = 0; 86 } 87 static int dev_open(int create) 88 { 89 dev_close(); 90 uint32_t bpu = 4096u / g_bs; 91 if (create) unlink(g_path); 92 memset(&g_f, 0, sizeof g_f); 93 if (disk_file_open(&g_f, &g_d, g_path, g_bs, (uint64_t)(A_UNITS + S_UNITS) * bpu, create) != 0) return -1; 94 g_open = 1; 95 if (st_disk_bind(&g_sd, &g_d, (uint64_t)A_UNITS * bpu, S_UNITS, &g_sdev) != 0) return -2; 96 ss_ts_device(&g_d, &g_tdev); 97 return 0; 98 } 99 static void make_keys(void) 100 { 101 uint8_t kvol[32]; 102 memset(kvol, 0x77, 32); 103 m5_subkeys sk; 104 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 105 memset(&g_k, 0, sizeof g_k); 106 g_k.identity_class = M5_ID_TEST; 107 g_k.key_generation = 1; 108 memcpy(g_k.k_root_auth, sk.k_root_auth, 32); 109 memcpy(g_k.k_domain, sk.k_artifact, 32); 110 m5_subkeys_wipe(&sk); 111 } 112 static void bind(void) 113 { 114 g_sk.s = &g_s; 115 g_sk.hook = NULL; 116 g_sk.hook_arg = NULL; 117 cr_bind_sealed(&g_src, &g_s); 118 cr_bind_sealed_sink(&g_snk, &g_sk); 119 } 120 static void fresh(void) 121 { 122 CK_(dev_open(1) == 0, "rig create"); 123 CK_(ss_format(&g_sdev, &g_tdev, 0, RUUID, &g_k, &g_ws) == 0, "format"); 124 CK_(ss_open(&g_s, &g_sdev, &g_tdev, 0, &g_k, &g_ws) == 0, "open genesis"); 125 bind(); 126 } 127 static void env_open(void) 128 { 129 CK_(dev_open(0) == 0, "reopen rig"); 130 E.dev = &g_sdev; 131 E.anchor = &g_tdev; 132 E.anchor_lba = 0; 133 E.keys = &g_k; 134 E.ws = &g_ws; 135 E.store = &g_s; 136 E.w = &g_w; 137 E.tw = &g_tw; 138 E.view = &g_v2; 139 } 140 static int file_load(uint8_t *dst) 141 { 142 dev_close(); 143 FILE *f = fopen(g_path, "rb"); 144 if (!f) return -1; 145 size_t n = fread(dst, 1, IMG_BYTES, f); 146 fclose(f); 147 return n == IMG_BYTES ? 0 : -2; 148 } 149 /* Flip one byte of ABSOLUTE image unit `abs_unit` (anchor units are 0..A_UNITS-1, 150 * Store-region unit u is A_UNITS + u). */ 151 static void flip_abs(uint64_t abs_unit, unsigned off) 152 { 153 dev_close(); 154 FILE *f = fopen(g_path, "r+b"); 155 CK_(f != NULL, "open image"); 156 if (!f) return; 157 long pos = (long)(abs_unit * 4096u + off); 158 uint8_t b = 0; 159 fseek(f, pos, SEEK_SET); 160 CK_(fread(&b, 1, 1, f) == 1, "read byte"); 161 b ^= 0xff; 162 fseek(f, pos, SEEK_SET); 163 CK_(fwrite(&b, 1, 1, f) == 1, "write byte"); 164 fclose(f); 165 } 166 167 static struct cc_record fact(const char *s) 168 { 169 struct cc_record r; 170 memset(&r, 0, sizeof r); 171 r.status = CC_EPI_DIRECT_OBSERVATION; 172 r.len = (uint16_t)strlen(s); 173 r.statement = (const uint8_t *)s; 174 return r; 175 } 176 177 /* Two mocked-RNDR helpers, as test_continuity_recovery.c. */ 178 struct mock { 179 int present; 180 uint64_t ctr; 181 }; 182 static struct mock g_m; 183 static struct ck_rng_ops g_ops; 184 static struct ck_rng g_rng; 185 static int m_present(void *c) { return ((struct mock *)c)->present; } 186 static int m_read(void *c, uint64_t *out) 187 { 188 struct mock *m = c; 189 uint64_t z = (m->ctr += 0x9e3779b97f4a7c15ull); 190 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ull; 191 z = (z ^ (z >> 27)) * 0x94d049bb133111ebull; 192 *out = z ^ (z >> 31); 193 return 1; 194 } 195 static void rng_init(uint64_t seed, int present) 196 { 197 g_m.present = present; 198 g_m.ctr = seed; 199 g_ops.present = m_present; 200 g_ops.read = m_read; 201 g_ops.ctx = &g_m; 202 memset(&g_rng, 0, sizeof g_rng); 203 (void)ck_rng_probe(&g_rng, &g_ops); 204 } 205 206 /* Provisioned with one remembered fact (two generations). */ 207 static void remembered(void) 208 { 209 fresh(); 210 rng_init(5005, 1); 211 const char *why = NULL; 212 int rc = 0; 213 CK_(cr_provision(&g_src, &g_snk, &g_w, &g_tw, &g_rng, RUUID, CC_SOURCE_QUALIFICATION, &g_v, &why, &rc) == CR_RESOLVED, 214 "provision"); 215 struct cc_record r1 = fact("keep this"); 216 struct cr_update up = {NULL, &r1, 1}; 217 static struct cr_view cur; 218 memcpy(&cur, &g_v, sizeof cur); 219 CK_(cr_commit(&g_src, &g_snk, &g_w, &g_tw, &cur, &up, 0, &g_v, &why, &rc) == CR_RESOLVED, "remember"); 220 } 221 222 static struct rc_record R, R2; 223 static void inspect(struct rc_record *r) 224 { 225 env_open(); 226 CK_(rc_inspect(&E, r) == 0, "inspect"); 227 } 228 /* SHA-256 over two units of the image, by the same primitive the Recovery Core uses. */ 229 static void digest_of_image_units(uint64_t ua, uint64_t ub, uint8_t out[32]) 230 { 231 CK_(file_load(img) == 0, "load image"); 232 cr_state_digest(img + ua * 4096u, img + ub * 4096u, out); 233 } 234 235 /* (1) digest comes from Store-region units 0 and 1, not from anchor units. */ 236 static void t_source(void) 237 { 238 remembered(); 239 uint8_t want[32], anchor_d[32]; 240 inspect(&R); 241 digest_of_image_units(A_UNITS, A_UNITS + 1u, want); 242 digest_of_image_units(0, 1, anchor_d); 243 CK_(memcmp(R.state_digest, want, 32) == 0, "state_digest == SHA-256(store unit 0, store unit 1)"); 244 CK_(memcmp(R.state_digest, anchor_d, 32) != 0, "state_digest is not the digest of anchor units 0,1"); 245 /* counterexample: swapping the two units gives a different digest, so order is bound too */ 246 uint8_t swapped[32]; 247 CK_(file_load(img) == 0, "load"); 248 cr_state_digest(img + (A_UNITS + 1u) * 4096u, img + A_UNITS * 4096u, swapped); 249 CK_(memcmp(swapped, want, 32) != 0, "unit order matters (swap differs)"); 250 dev_close(); 251 } 252 253 /* (2) a flip in Store-region unit 0, and separately unit 1, changes the digest. */ 254 static void t_store_flip(unsigned u) 255 { 256 remembered(); 257 inspect(&R); 258 uint8_t c0[32]; 259 int have0 = rc_challenge(&R, CR_ACTION_REPAIR_DEGRADED_PEER, c0); 260 flip_abs(A_UNITS + u, 100); 261 inspect(&R2); 262 uint8_t c1[32]; 263 int have1 = rc_challenge(&R2, CR_ACTION_REPAIR_DEGRADED_PEER, c1); 264 CK_(memcmp(R.state_digest, R2.state_digest, 32) != 0, "flip in store unit %u changes the digest", u); 265 uint8_t want[32]; 266 digest_of_image_units(A_UNITS, A_UNITS + 1u, want); 267 CK_(memcmp(R2.state_digest, want, 32) == 0, "new digest still equals SHA-256 of store units 0,1"); 268 if (have0 == 1 && have1 == 1) CK_(memcmp(c0, c1, 32) != 0, "challenge changes with store unit %u", u); 269 /* Which slot is inactive is known only after the fact: the digest check above is 270 * the load-bearing one; the challenge is compared whenever both exist. */ 271 dev_close(); 272 } 273 274 /* The inactive slot is Store unit 1 - active; flipping it is a degraded mount that still 275 * yields a challenge (contract K-5), so the challenge change is asserted unconditionally. */ 276 static void t_inactive_challenge(void) 277 { 278 remembered(); 279 inspect(&R); 280 uint32_t inactive = 1u - st_active_slot(&g_s.st); 281 uint8_t c0[32], c1[32]; 282 CK_(R.have_uuid && R.have_mount, "baseline has uuid and mount"); 283 int a = rc_challenge(&R, CR_ACTION_REPAIR_DEGRADED_PEER, c0); 284 flip_abs(A_UNITS + inactive, 100); 285 inspect(&R2); 286 int b = rc_challenge(&R2, CR_ACTION_REPAIR_DEGRADED_PEER, c1); 287 CK_(a == 1 && b == 1, "challenge exists before and after an inactive-slot flip"); 288 CK_(memcmp(c0, c1, 32) != 0, "challenge changes after flipping the inactive slot (store unit %u)", inactive); 289 dev_close(); 290 } 291 292 /* (3) a flip in any anchor unit leaves the digest unchanged. */ 293 static void t_anchor_flip(unsigned a) 294 { 295 remembered(); 296 inspect(&R); 297 uint8_t before[32]; 298 memcpy(before, R.state_digest, 32); 299 uint8_t keep[4096]; 300 CK_(file_load(img) == 0, "load"); 301 memcpy(keep, img + (size_t)a * 4096u, 4096); 302 flip_abs(a, 100); 303 /* counterexample: the flip is not a no-op */ 304 CK_(file_load(img) == 0 && memcmp(keep, img + (size_t)a * 4096u, 4096) != 0, "anchor unit %u byte really flips", a); 305 inspect(&R2); 306 CK_(memcmp(before, R2.state_digest, 32) == 0, "anchor unit %u flip does not change the digest", a); 307 dev_close(); 308 } 309 310 /* (4) layout constants and Store-region arithmetic (dev/disk_layout.h). */ 311 static void t_layout(void) 312 { 313 CK_(CK_LAYOUT_UNIT == 4096u, "unit 4096"); 314 CK_(CK_LAYOUT_ANCHOR_UNITS == 4u, "anchor 4 units"); 315 CK_(CK_LAYOUT_PROBE_UNITS == 1u, "probe 1 unit"); 316 CK_(CK_LAYOUT_MIN_STORE_UNITS == 64u, "min store 64 units"); 317 static const uint32_t Us[] = {CK_LAYOUT_ANCHOR_UNITS + CK_LAYOUT_MIN_STORE_UNITS + CK_LAYOUT_PROBE_UNITS, 1000u, 65536u}; 318 for (unsigned i = 0; i < sizeof Us / sizeof Us[0]; i++) { 319 uint32_t U = Us[i]; 320 uint32_t first = CK_LAYOUT_ANCHOR_UNITS; /* store region 4..U-2 */ 321 uint32_t last = U - CK_LAYOUT_PROBE_UNITS - 1u; /* U-2 */ 322 uint32_t n = U - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS; 323 CK_(first == 4u, "store region starts at unit 4 (U=%u)", U); 324 CK_(last == U - 2u, "store region ends at unit U-2 (U=%u)", U); 325 CK_(last - first + 1u == n, "store region has U-5 units (U=%u)", U); 326 CK_(n == U - 5u, "U-5 (U=%u)", U); 327 CK_(n >= CK_LAYOUT_MIN_STORE_UNITS, "meets the 64 unit minimum (U=%u)", U); 328 /* store unit 0 is image unit 4 (first raw unit hashed), probe is unit U-1 */ 329 CK_(first + 0u == 4u && first + 1u == 5u, "raw units 0,1 are partition units 4,5"); 330 } 331 /* counterexample: one unit fewer than the minimum is below it */ 332 uint32_t Ubad = CK_LAYOUT_ANCHOR_UNITS + CK_LAYOUT_MIN_STORE_UNITS + CK_LAYOUT_PROBE_UNITS - 1u; 333 CK_(Ubad - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS < CK_LAYOUT_MIN_STORE_UNITS, "U-1 is below the minimum"); 334 /* this test's own rig: anchor units, then the Store region, as the layout */ 335 CK_(T_UNITS == A_UNITS + S_UNITS && S_UNITS >= CK_LAYOUT_MIN_STORE_UNITS, "rig geometry"); 336 } 337 338 int main(void) 339 { 340 make_keys(); 341 snprintf(g_path, sizeof g_path, "/tmp/ck_geo_test_%d.img", (int)getpid()); 342 t_layout(); 343 static const uint32_t geos[2] = {4096u, 512u}; 344 for (int g = 0; g < 2; g++) { 345 g_bs = geos[g]; 346 t_source(); 347 t_store_flip(0); 348 t_store_flip(1); 349 t_inactive_challenge(); 350 for (unsigned a = 0; a < A_UNITS; a++) t_anchor_flip(a); 351 } 352 dev_close(); 353 unlink(g_path); 354 return ck_t_verdict("test_continuity_geometry"); 355 }