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_fact_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_fact_test.c` 256 lines ยท 10917 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_fact_test.c -- AIENOS PI5-5 host test: the Pi Cortex fact stage (svc/pi5_fact.c, hooked by 18 * svc/store_boot.c ck_store_pi5_fact_stage, compiled with -DCK_PI5_UNLOCK -DCK_PI5_UNLOCK_CARD -DCK_PI5_FACT 19 * exactly as "make pi5-fact" builds it) over the Pi card layout. A file stands in for the card; every "boot" 20 * reopens the file and starts from a cold C state (no Store or Cortex object survives between boots). 21 * 22 * Proves: boot 1 (card fact request) stores a fact into the sealed Store through the Cortex journal and 23 * prints it with the Store generation that committed it; boot 2 (no request) recalls the same text and the 24 * same generation from the disk, writes no new fact and verifies the chain; a third boot with a second fact 25 * keeps both in order; a boot with the wrong recovery secret recalls nothing, prints no fact text and writes 26 * nothing; the right secret afterwards still recalls; a card with no journal says "none" and creates none; 27 * the request file checker refuses every malformed layout. Built with -DCK_FACT_MUTANT_NO_APPEND (the commit 28 * skipped) or -DCK_FACT_MUTANT_RECALL_BLIND (recall never reads the journal) this test must FAIL. 29 * Hosted test code only. */ 30 #include 31 #include 32 #include 33 #include 34 #include "ck.h" 35 #include "ck_host.h" 36 #include "disk_file.h" 37 #include "disk_layout.h" 38 #include "disk_part.h" 39 #include "gpt_write.h" 40 #include "../../core/pi5_marker.h" 41 #include "m5.h" 42 #include "store_boot.h" 43 #include "../../core/unlock_req.h" 44 #include "../../core/fact_req.h" 45 #include "../../svc/pi5_fact.h" 46 47 #if !defined(CK_PI5_UNLOCK) || !defined(CK_PI5_FACT) 48 #error "pi5_fact_test.c must be built with -DCK_PI5_UNLOCK -DCK_PI5_FACT (the image's policy)" 49 #endif 50 51 static int checks, failures; 52 #define CHECK(c) \ 53 do { \ 54 checks++; \ 55 if (!(c)) { \ 56 failures++; \ 57 printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \ 58 } \ 59 } while (0) 60 61 extern int ck_host_entropy_refuse; 62 int ck_pi5_unlock_secret(uint8_t out[CK_SB_SECRET_LEN], const char **source) 63 { 64 (void)out; 65 (void)source; 66 return -1; 67 } 68 69 #define BLOCKS 262144ull 70 #define PART_FIRST 8192u 71 #define PART_SECS 8192u 72 static char g_path[256]; 73 74 /* One boot: open the card file, run the Store stage exactly as ck_stage_store does (unlock policy, then the 75 * fact hook), close the card. text NULL: no request file this boot (the stub says ABSENT). Output captured. */ 76 static int boot(const uint8_t *secret, const char *text, ck_store_report *r, char **cap) 77 { 78 disk_file f; 79 disk_dev d; 80 ck_part p, sp; 81 ck_gpt_info gi; 82 if (disk_file_open(&f, &d, g_path, 512, 0, 0)) return -9999; 83 if (ck_gpt_find_aienos(&d, &p, &gi) || ck_pi5_store_view(&p, &sp)) { disk_file_close(&f); return -9998; } 84 memset(&ck_fact_req_boot, 0, sizeof ck_fact_req_boot); 85 if (text) { 86 struct ck_fact_req q; 87 const char *why = 0; 88 memset(&q, 0, sizeof q); 89 memcpy(q.magic, CK_FACT_REQ_MAGIC, 8); 90 q.version = CK_FACT_REQ_VERSION; 91 memcpy(q.tag, CK_FACT_REQ_TAG, 16); 92 q.len = (uint32_t)strlen(text); 93 memcpy(q.text, text, q.len); 94 CHECK(ck_fact_req_check(&q, &why) == 0); 95 ck_fact_req_store(&q); 96 } else { 97 ck_fact_req_boot.state = CK_FACT_REQ_ABSENT; 98 } 99 ck_host_quiet = 1; 100 ck_host_capture_start(); 101 int rc = ck_store_pi5_unlock_run(&sp.dev, secret, "pi5-fact-test", r); 102 int frc = ck_store_pi5_fact_stage(r); 103 CHECK(frc == 0); 104 *cap = strdup(ck_host_capture_text()); 105 ck_host_capture_stop(); 106 ck_host_quiet = 0; 107 CHECK(ck_fact_req_boot.state != CK_FACT_REQ_FOUND); /* the request is single use per boot */ 108 disk_file_close(&f); 109 return rc; 110 } 111 112 static uint8_t *slurp(size_t *n) 113 { 114 FILE *fp = fopen(g_path, "rb"); 115 CHECK(fp != 0); 116 if (!fp) { *n = 0; return 0; } 117 fseek(fp, 0, SEEK_END); 118 *n = (size_t)ftell(fp); 119 rewind(fp); 120 uint8_t *m = malloc(*n); 121 CHECK(m && fread(m, 1, *n, fp) == *n); 122 fclose(fp); 123 return m; 124 } 125 126 /* "key=NNN" after the first occurrence of `after` in s; 0 when absent. */ 127 static unsigned long long num_after(const char *s, const char *after, const char *key) 128 { 129 const char *p = strstr(s, after); 130 if (!p) return 0; 131 p = strstr(p, key); 132 if (!p) return 0; 133 return strtoull(p + strlen(key), 0, 10); 134 } 135 136 static void req_with(struct ck_fact_req *q) 137 { 138 memset(q, 0, sizeof *q); 139 memcpy(q->magic, CK_FACT_REQ_MAGIC, 8); 140 q->version = CK_FACT_REQ_VERSION; 141 memcpy(q->tag, CK_FACT_REQ_TAG, 16); 142 q->len = 5; 143 memcpy(q->text, "hello", 5); 144 } 145 146 int main(void) 147 { 148 snprintf(g_path, sizeof g_path, "/tmp/aienos-pi5-fact-test-%d.img", (int)getpid()); 149 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}}; 150 CHECK(gw_write(g_path, 512, BLOCKS, gp, 2, 0) == 0); 151 uint8_t S[CK_SB_SECRET_LEN], W[CK_SB_SECRET_LEN]; 152 for (int i = 0; i < 32; i++) S[i] = (uint8_t)(i * 7 + 5); 153 memcpy(W, S, 32); 154 W[31] ^= 0x80; 155 ck_store_report r; 156 char *c1, *c2, *c3, *c4, *c5; 157 size_t n0, n1; 158 uint8_t *b, *a; 159 const char *FACT = "prefer short notes"; 160 161 /* 0. The request file checker. */ 162 { 163 struct ck_fact_req q; 164 const char *why = 0; 165 req_with(&q); 166 CHECK(ck_fact_req_check(&q, &why) == 0 && why == 0); 167 req_with(&q); q.magic[0] = 'X'; CHECK(ck_fact_req_check(&q, &why) != 0); 168 req_with(&q); q.version = 2; CHECK(ck_fact_req_check(&q, &why) != 0); 169 req_with(&q); q.flags = 1; CHECK(ck_fact_req_check(&q, &why) != 0); 170 req_with(&q); q.tag[0] = 'q'; CHECK(ck_fact_req_check(&q, &why) != 0); 171 req_with(&q); q.len = 0; CHECK(ck_fact_req_check(&q, &why) != 0); 172 req_with(&q); q.len = 49; CHECK(ck_fact_req_check(&q, &why) != 0); 173 req_with(&q); q.reserved = 1; CHECK(ck_fact_req_check(&q, &why) != 0); 174 req_with(&q); q.text[1] = 0x07; CHECK(ck_fact_req_check(&q, &why) != 0); 175 req_with(&q); q.text[7] = 'z'; CHECK(ck_fact_req_check(&q, &why) != 0); 176 req_with(&q); q.pad[0] = 1; CHECK(ck_fact_req_check(&q, &why) != 0); 177 req_with(&q); q.len = 48; memset(q.text, 'a', 48); CHECK(ck_fact_req_check(&q, &why) == 0); 178 CHECK(ck_fact_req_check(0, &why) != 0); 179 } 180 181 /* 1. Blank card, no request: the Store is created, recall says none, no journal is created. */ 182 CHECK(boot(S, 0, &r, &c1) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 1); 183 CHECK(strstr(c1, "fact_stage: recall none cortex=none records=0")); 184 CHECK(!strstr(c1, "cortex open")); 185 free(c1); 186 187 /* 2. Boot 1 (F1): the card request stores a fact through the Cortex journal. */ 188 CHECK(boot(S, FACT, &r, &c1) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 0); 189 CHECK(strstr(c1, "fact_stage: cortex open records=0 chunks=1")); 190 CHECK(strstr(c1, "fact_stage: remembered id=1 t=1 ")); 191 CHECK(strstr(c1, "bytes=18 text=\"prefer short notes\"")); 192 CHECK(strstr(c1, "fact_stage: readback id=1 t=1 ")); 193 CHECK(strstr(c1, "fact_stage: remember records=1 shown=1 newest_id=1")); 194 CHECK(strstr(c1, "chain=ok")); 195 unsigned long long gen1 = num_after(c1, "fact_stage: remembered", "store_gen="); 196 CHECK(gen1 > 0); 197 CHECK(num_after(c1, "fact_stage: readback", "store_gen=") == gen1); 198 199 /* 3. Boot 2 (F2): cold start, no request file: the same fact and generation come back from the disk. */ 200 b = slurp(&n0); 201 CHECK(boot(S, 0, &r, &c2) == 0 && r.verdict == CK_SB_COMMITTED && r.formatted == 0); 202 CHECK(strstr(c2, "fact_stage: cortex open records=1 chunks=2")); 203 CHECK(strstr(c2, "fact_stage: recalled id=1 t=1 ")); 204 CHECK(strstr(c2, "text=\"prefer short notes\"")); 205 CHECK(num_after(c2, "fact_stage: recalled", "store_gen=") == gen1); 206 CHECK(strstr(c2, "fact_stage: recall records=1 shown=1 newest_id=1")); 207 CHECK(strstr(c2, "chain=ok")); 208 CHECK(!strstr(c2, "remembered") && !strstr(c2, "readback")); 209 a = slurp(&n1); 210 CHECK(n0 == n1); /* a recall adds no fact; only the boot record advanced */ 211 free(a); 212 free(b); 213 214 /* 4. Negative control (F3): the wrong recovery secret opens nothing, recalls nothing, writes nothing. */ 215 b = slurp(&n0); 216 CHECK(boot(W, 0, &r, &c3) == CK_SB_E_SECRET_REJECTED && r.verdict == CK_SB_REFUSED); 217 a = slurp(&n1); 218 CHECK(n0 == n1 && memcmp(b, a, n0) == 0); 219 CHECK(strstr(c3, "fact_stage: NOTHING RECALLED")); 220 CHECK(!strstr(c3, "prefer short notes") && !strstr(c3, "recalled id=") && !strstr(c3, "cortex open")); 221 free(a); 222 free(b); 223 /* a wrong secret together with a request file also writes nothing (the request is dropped) */ 224 b = slurp(&n0); 225 CHECK(boot(W, "must not be stored", &r, &c4) == CK_SB_E_SECRET_REJECTED); 226 a = slurp(&n1); 227 CHECK(memcmp(b, a, n0) == 0 && strstr(c4, "NOTHING RECALLED") && !strstr(c4, "must not be stored")); 228 free(a); 229 free(b); 230 231 /* 5. The right secret still recalls after the refusals; a second fact keeps both, in order. */ 232 CHECK(boot(S, "second fact", &r, &c5) == 0); 233 CHECK(strstr(c5, "fact_stage: remembered id=2 t=2 ")); 234 CHECK(strstr(c5, "fact_stage: readback id=1 ") && strstr(c5, "fact_stage: readback id=2 ")); 235 free(c5); 236 CHECK(boot(S, 0, &r, &c5) == 0); 237 { 238 const char *p1 = strstr(c5, "recalled id=1 "); 239 const char *p2 = strstr(c5, "recalled id=2 "); 240 CHECK(p1 && p2 && p1 < p2); 241 CHECK(strstr(c5, "text=\"prefer short notes\"") && strstr(c5, "text=\"second fact\"")); 242 CHECK(strstr(c5, "fact_stage: recall records=2 shown=2")); 243 } 244 free(c5); 245 246 free(c1); 247 free(c2); 248 free(c3); 249 free(c4); 250 memset(S, 0, sizeof S); 251 memset(W, 0, sizeof W); 252 unlink(g_path); 253 printf("pi5_fact_test: %d checks, %d failures\n", checks, failures); 254 printf("CK_PI5_FACT_HOST: %s\n", failures ? "FAIL" : "PASS"); 255 return failures ? 1 : 0; 256 }