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/dev/tests/pi5_unlock_test.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 / dev / tests / pi5_unlock_test.c` 336 lines ยท 14481 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 /* pi5_unlock_test.c -- AIENOS PI5-4 cut 1 host test: the Pi unlock image's Store policy 18 * (svc/store_boot.c ck_store_pi5_unlock_run, compiled with -DCK_PI5_UNLOCK exactly as 19 * "make pi5-unlock" builds it) over the Pi card layout. A file stands in for the card. 20 * 21 * Proves: no secret -> refused before the disk is read, nothing written; TEST-identity keys are 22 * refused by the image's own policy (store_boot_run) before any disk access, on a blank card and on 23 * a PRODUCTION Store; a TEST-tier Store (formatted with TEST keys and the TEST uuid by a different 24 * image) is refused by the unlock path with no keyslot manifest, nothing written; a wrong secret 25 * is refused, nothing written; the right secret on a blank card formats a PRODUCTION-class Store 26 * under a drawn uuid that is not the TEST uuid; the same secret reopens it and the generation 27 * advances. The TEST keys are built here from the public label, because the image itself does not 28 * contain them (scripts/ck_pi5_build_check.sh checks that on the real ELF). Hosted test code only. */ 29 #include 30 #include 31 #include 32 #include 33 #include "ck.h" 34 #include "ck_host.h" 35 #include "disk_file.h" 36 #include "disk_layout.h" 37 #include "disk_part.h" 38 #include "gpt_write.h" 39 #include "../../core/pi5_marker.h" 40 #include "m5.h" 41 #include "sha256.h" 42 #include "store_boot.h" 43 #include "../../tools/pi5_store_locate.h" 44 #include "../../core/unlock_req.h" 45 46 #ifndef CK_PI5_UNLOCK 47 #error "pi5_unlock_test.c must be built with -DCK_PI5_UNLOCK (the image's policy)" 48 #endif 49 #ifdef CK_STORE_TEST_KEYS 50 #error "the unlock image must not compile the TEST keys" 51 #endif 52 53 static int checks, failures; 54 #define CHECK(c) \ 55 do { \ 56 checks++; \ 57 if (!(c)) { \ 58 failures++; \ 59 printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ 60 } \ 61 } while (0) 62 63 extern int ck_host_entropy_refuse; 64 65 /* The secret source symbol the stage references; the real one lives in core/pi5.c. */ 66 int ck_pi5_unlock_secret(uint8_t out[CK_SB_SECRET_LEN], const char **source) 67 { 68 (void)out; 69 (void)source; 70 return -1; 71 } 72 73 #define BLOCKS 262144ull 74 #define PART_FIRST 8192u 75 #define PART_SECS 8192u 76 static char g_path[256]; 77 78 /* The public TEST keys and uuid of the SEED-0B-PI5 image, rebuilt here (public label, not secret). */ 79 static const uint8_t test_uuid[16] = {'A', 'I', 'E', 'N', '-', 'T', 'E', 'S', 'T', '-', 'B', 'O', 'O', 'T', '0', '1'}; 80 static void test_keys(ss_keys *k) 81 { 82 uint8_t kvol[32]; 83 m5_subkeys sk; 84 static const char label[] = "AIENOS-LANE18-TEST-KVOL-NOT-SECRET"; 85 sha256_hash((const uint8_t *)label, sizeof label - 1, kvol); 86 memset(k, 0, sizeof *k); 87 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 88 k->identity_class = M5_ID_TEST; 89 k->key_generation = 1; 90 memcpy(k->k_root_auth, sk.k_root_auth, 32); 91 memcpy(k->k_domain, sk.k_artifact, 32); 92 m5_subkeys_wipe(&sk); 93 } 94 95 static uint8_t *slurp(size_t *n) 96 { 97 FILE *fp = fopen(g_path, "rb"); 98 CHECK(fp != 0); 99 if (!fp) { *n = 0; return 0; } 100 fseek(fp, 0, SEEK_END); 101 *n = (size_t)ftell(fp); 102 rewind(fp); 103 uint8_t *m = malloc(*n); 104 CHECK(m && fread(m, 1, *n, fp) == *n); 105 fclose(fp); 106 return m; 107 } 108 109 enum { BOOT_TESTKEYS_POLICY, BOOT_TESTKEYS_IMAGE, BOOT_UNLOCK }; 110 /* One boot on the Store view of the card file. */ 111 static int boot(int mode, const uint8_t *secret, ck_store_report *r) 112 { 113 disk_file f; 114 disk_dev d; 115 ck_part p, sp; 116 ck_gpt_info gi; 117 ss_keys k; 118 if (disk_file_open(&f, &d, g_path, 512, 0, 0)) return -9999; 119 if (ck_gpt_find_aienos(&d, &p, &gi) || ck_pi5_store_view(&p, &sp)) { disk_file_close(&f); return -9998; } 120 ck_host_quiet = 1; 121 int rc; 122 test_keys(&k); 123 if (mode == BOOT_TESTKEYS_POLICY) rc = store_boot_run_policy(&sp.dev, &k, test_uuid, "pi5-unlock-test", 0, r); 124 else if (mode == BOOT_TESTKEYS_IMAGE) rc = store_boot_run(&sp.dev, &k, test_uuid, "pi5-unlock-test", r); 125 else rc = ck_store_pi5_unlock_run(&sp.dev, secret, "pi5-unlock-test", r); 126 ck_host_quiet = 0; 127 memset(&k, 0, sizeof k); 128 disk_file_close(&f); 129 return rc; 130 } 131 132 static int same_bytes(const uint8_t *a, size_t na, const uint8_t *b, size_t nb) 133 { 134 return a && b && na == nb && memcmp(a, b, na) == 0; 135 } 136 137 int main(void) 138 { 139 snprintf(g_path, sizeof g_path, "/tmp/aienos-pi5-unlock-test-%d.img", (int)getpid()); 140 gw_part gp[2] = {{gw_esp_type, 2048, 4095, "AIENOSPI5-ESP", 0x5a}, {ck_aienos_part_type, PART_FIRST, PART_FIRST + PART_SECS - 1, "AIENOS", 0}}; 141 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); 142 uint8_t S[CK_SB_SECRET_LEN], W[CK_SB_SECRET_LEN]; 143 uint8_t S0[CK_SB_SECRET_LEN]; 144 for (int i = 0; i < 32; i++) S[i] = (uint8_t)(i * 11 + 3); 145 memcpy(S0, S, 32); 146 memcpy(W, S, 32); 147 W[31] ^= 0x80; 148 ck_store_report r; 149 size_t n0, n1; 150 uint8_t *b, *a; 151 152 /* 1. No secret source: refused before the disk is read; a blank card stays blank. */ 153 b = slurp(&n0); 154 int rc = boot(BOOT_UNLOCK, 0, &r); 155 a = slurp(&n1); 156 CHECK(rc == CK_SB_E_BLOCKED_OPERATOR && r.verdict == CK_SB_REFUSED && r.formatted == 0); 157 CHECK(r.proof && strcmp(r.proof, "keyed") == 0); 158 CHECK(same_bytes(b, n0, a, n1)); 159 free(b); 160 free(a); 161 162 /* 2. TEST keys against the image's own policy (hardware policy): refused before any disk access. */ 163 b = slurp(&n0); 164 rc = boot(BOOT_TESTKEYS_IMAGE, 0, &r); 165 a = slurp(&n1); 166 CHECK(rc == CK_SB_E_TEST_KEYS && r.verdict == CK_SB_REFUSED && r.formatted == 0); 167 CHECK(same_bytes(b, n0, a, n1)); 168 free(b); 169 free(a); 170 171 /* 3. A TEST-tier Store (made by the SEED-0B-PI5 image's policy, TEST keys + TEST uuid) is not a 172 * production Store: the unlock path finds no keyslot manifest and refuses, nothing written. */ 173 rc = boot(BOOT_TESTKEYS_POLICY, 0, &r); 174 CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1 && r.identity_class == M5_ID_TEST); 175 b = slurp(&n0); 176 rc = boot(BOOT_UNLOCK, S, &r); 177 a = slurp(&n1); 178 CHECK(rc == CK_SB_E_NO_KEYSLOT && r.verdict == CK_SB_REFUSED && r.formatted == 0); 179 CHECK(same_bytes(b, n0, a, n1)); 180 free(b); 181 free(a); 182 rc = boot(BOOT_TESTKEYS_IMAGE, 0, &r); /* and the image policy still refuses TEST keys on a Store that has TEST content */ 183 CHECK(rc == CK_SB_E_TEST_KEYS); 184 185 /* Fresh blank card for the production path. */ 186 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); 187 188 /* 4. Blank card, entropy refused: no genesis, nothing written. */ 189 b = slurp(&n0); 190 ck_host_entropy_refuse = 1; 191 rc = boot(BOOT_UNLOCK, S, &r); 192 ck_host_entropy_refuse = 0; 193 a = slurp(&n1); 194 CHECK(rc == CK_SB_E_ENTROPY && r.formatted == 0 && same_bytes(b, n0, a, n1)); 195 free(b); 196 free(a); 197 198 /* 5. Blank card, secret S: formatted PRODUCTION-class Store under a drawn uuid (not the TEST uuid). */ 199 rc = boot(BOOT_UNLOCK, S, &r); 200 CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1 && r.unlocked_genesis == 1); 201 CHECK(r.identity_class == M5_ID_PRODUCTION && r.boot_count_new == 1); 202 CHECK(r.key_source && strcmp(r.key_source, "recovery-keyslot") == 0); 203 CHECK(store_boot_store()->has_keyslot == 1 && store_boot_store()->keys.identity_class == M5_ID_PRODUCTION); 204 CHECK(memcmp(store_boot_store()->uuid, test_uuid, 16) != 0); 205 uint64_t g1 = r.gen_commit; 206 207 /* 6. Wrong secret: refused, nothing written. */ 208 b = slurp(&n0); 209 rc = boot(BOOT_UNLOCK, W, &r); 210 a = slurp(&n1); 211 CHECK(rc == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED && r.formatted == 0); 212 CHECK(same_bytes(b, n0, a, n1)); 213 free(b); 214 free(a); 215 216 /* 7. TEST keys on the production Store: refused by policy, nothing written. */ 217 b = slurp(&n0); 218 rc = boot(BOOT_TESTKEYS_IMAGE, 0, &r); 219 a = slurp(&n1); 220 CHECK(rc == CK_SB_E_TEST_KEYS && same_bytes(b, n0, a, n1)); 221 free(b); 222 free(a); 223 224 /* 8. Reboot, secret S: reopens, no reformat, generation advances, boot count 2. */ 225 rc = boot(BOOT_UNLOCK, S, &r); 226 CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 0 && r.unlocked_genesis == 0); 227 CHECK(r.boot_count_prev == 1 && r.boot_count_new == 2 && r.gen_open == g1 && r.gen_commit > g1); 228 CHECK(strcmp(r.prev_commit, "pi5-unlock-test") == 0 && r.identity_class == M5_ID_PRODUCTION); 229 CHECK(memcmp(store_boot_store()->uuid, test_uuid, 16) != 0); 230 231 /* 9. Card request file (cut 2) end to end: a malformed request gives no secret and the blank card 232 * stays blank; a well-formed one is taken once, formats on the first run and reopens on the second 233 * (each "run" has its own boot-time request, as pi5-run.sh places it). */ 234 { 235 struct ck_unlock_req q; 236 uint8_t got[CK_SB_SECRET_LEN]; 237 const char *why = 0; 238 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); 239 memset(&q, 0, sizeof q); 240 memcpy(q.magic, CK_UNLOCK_REQ_MAGIC, 8); 241 q.version = CK_UNLOCK_REQ_VERSION; 242 memcpy(q.tag, "QUALIF-THROWAWAY", 16); 243 for (int i = 0; i < 32; i++) q.secret[i] = (uint8_t)(0x90 ^ (i * 5)); 244 q.tag[0] = 'q'; /* not the tier tag: the stub refuses it, nothing is stored */ 245 memset(&ck_unlock_req_boot, 0, sizeof ck_unlock_req_boot); 246 CHECK(ck_unlock_req_check(&q, &why) != 0); 247 b = slurp(&n0); 248 int have = ck_unlock_req_take(got) == 0; 249 rc = boot(BOOT_UNLOCK, have ? got : 0, &r); 250 a = slurp(&n1); 251 CHECK(!have && rc == CK_SB_E_BLOCKED_OPERATOR && same_bytes(b, n0, a, n1)); 252 free(b); 253 free(a); 254 q.tag[0] = 'Q'; 255 for (int run = 0; run < 2; run++) { 256 CHECK(ck_unlock_req_check(&q, &why) == 0); 257 ck_unlock_req_store(&q); 258 CHECK(ck_unlock_req_take(got) == 0); 259 rc = boot(BOOT_UNLOCK, got, &r); 260 CHECK(rc == 0 && r.verdict == CK_SB_COMMITTED && r.identity_class == M5_ID_PRODUCTION); 261 CHECK(r.formatted == (run == 0) && r.boot_count_new == (uint64_t)run + 1); 262 CHECK(ck_unlock_req_take(got) == -1); /* the request is single use per boot */ 263 } 264 memset(&q, 0, sizeof q); 265 memset(got, 0, sizeof got); 266 } 267 268 /* 10. Tamper rehearsal of the Pi runs C/D/E. Fresh card, secret S: genesis, then reopen. The locator 269 * (tools/pi5_store_locate.c) finds the KeySlotManifest on a copy of the partition exactly as the hub 270 * run does. One flipped byte of the wrapped K_vol, then of its tag: each is refused, nothing is 271 * written, no reformat; with the bytes restored the same secret reopens and the generation advances. */ 272 { 273 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); 274 rc = boot(BOOT_UNLOCK, S0, &r); 275 CHECK(rc == 0 && r.formatted == 1 && r.boot_count_new == 1); 276 rc = boot(BOOT_UNLOCK, S0, &r); 277 CHECK(rc == 0 && r.formatted == 0 && r.boot_count_new == 2); 278 uint64_t gclean = r.gen_commit; 279 size_t nc; 280 uint8_t *card = slurp(&nc); 281 char ppath[300]; 282 snprintf(ppath, sizeof ppath, "%s.part", g_path); 283 FILE *pf = fopen(ppath, "wb"); 284 CHECK(card && pf && fwrite(card + (size_t)PART_FIRST * 512u, 1, (size_t)PART_SECS * 512u, pf) == (size_t)PART_SECS * 512u); 285 if (pf) fclose(pf); 286 ck_pi5_ksm_loc loc; 287 char err[160] = ""; 288 int lrc = ck_pi5_locate_keyslot(ppath, &loc, err, sizeof err); 289 CHECK(lrc == 0); 290 if (lrc) printf(" locate: %s\n", err); 291 CHECK(loc.keyslot_objects == 1 && loc.byte_length == 640); 292 CHECK(loc.wrapped_kvol_offset == loc.part_byte_offset + 312u && loc.wrap_tag_offset == loc.part_byte_offset + 344u); 293 unlink(ppath); 294 uint64_t targets[2] = {loc.wrapped_kvol_offset, loc.wrap_tag_offset}; 295 for (int t = 0; t < 2 && lrc == 0; t++) { 296 size_t at = (size_t)PART_FIRST * 512u + (size_t)targets[t]; 297 CHECK(at < nc); 298 FILE *fp = fopen(g_path, "r+b"); 299 CHECK(fp != 0); 300 if (!fp) break; 301 uint8_t byte = card[at] ^ 0xFFu; 302 fseek(fp, (long)at, SEEK_SET); 303 CHECK(fwrite(&byte, 1, 1, fp) == 1); 304 fclose(fp); 305 b = slurp(&n0); 306 rc = boot(BOOT_UNLOCK, S0, &r); 307 a = slurp(&n1); 308 CHECK(rc != 0 && r.verdict == CK_SB_REFUSED && r.formatted == 0); 309 CHECK(same_bytes(b, n0, a, n1)); /* refused: not one byte written, no reformat */ 310 printf(" tamper %s: refused rc=%d proof=%s step=\"%s\"\n", t ? "wrap tag" : "wrapped K_vol", rc, 311 r.proof ? r.proof : "?", r.step ? r.step : "?"); 312 free(b); 313 free(a); 314 fp = fopen(g_path, "r+b"); /* restore the original byte */ 315 CHECK(fp != 0); 316 if (!fp) break; 317 fseek(fp, (long)at, SEEK_SET); 318 CHECK(fwrite(&card[at], 1, 1, fp) == 1); 319 fclose(fp); 320 } 321 uint8_t *rest = slurp(&n1); 322 CHECK(same_bytes(card, nc, rest, n1)); /* bytes restored: identical to the clean card */ 323 free(rest); 324 rc = boot(BOOT_UNLOCK, S0, &r); 325 CHECK(rc == 0 && r.formatted == 0 && r.boot_count_new == 3 && r.gen_open == gclean && r.gen_commit > gclean); 326 free(card); 327 } 328 329 memset(S0, 0, sizeof S0); 330 memset(S, 0, sizeof S); 331 memset(W, 0, sizeof W); 332 unlink(g_path); 333 printf("pi5_unlock_test: %d checks, %d failures\n", checks, failures); 334 printf("CK_PI5_UNLOCK_HOST: %s\n", failures ? "FAIL" : "PASS"); 335 return failures ? 1 : 0; 336 }